I have a function that uses reflection to set properties of object A from object B. At one point, I need to instantiate a generic collection. However, I am unable to get it work
One technique is to declare a static method taking the same generic arguments as the list you want to create. Then you can pull the arguments from the property to invoke the method.
Type interfaceType = destProperty.PropertyType;
return typeof(MyClass)
.GetMethod("CreateList", BindingFlags.NonPublic | BindingFlags.Static)
.MakeGenericMethod(interfaceType.GetGenericArguments())
.Invoke(null, new object[] { });
private static IList CreateList()
{
return new List();
}