Count
is a property, not a method:
var prop = anobj.GetProperty("Count");
Console.WriteLine(prop.GetValue(data, null));
However, it would be better to cast to the non-generic IList
:
var data = (IList)InstantiateGeneric("System.Collections.Generic.List`1",
"System.String");
Console.WriteLine(data.Count);
I also suggest talking in terms of Type
, not magic strings:
var itemType = typeof(string); // from somewhere, perhaps external
var listType = typeof(List<>).MakeGenericType(itemType);
var data = (IList)Activator.CreateInstance(listType);
Console.WriteLine(data.Count);