Let say I have a generic member in a class or method, so:
public class Foo
{
public List Bar { get; set; }
public void Baz()
{
This is how i did it
internal static Type GetElementType(this Type type)
{
//use type.GenericTypeArguments if exist
if (type.GenericTypeArguments.Any())
return type.GenericTypeArguments.First();
return type.GetRuntimeProperty("Item").PropertyType);
}
Then call it like this
var item = Activator.CreateInstance(iListType.GetElementType());
OR
var item = Activator.CreateInstance(Bar.GetType().GetElementType());