In a generic filer class which knows nothing about the calling assembly's data structures except through reflection, I need to create an instance of a List<T>. I can create simple instances (ints, strings, class instances etc) but not arrays, for two reasons:
1. I can use Assembly.GetType(typeName)
to get a Type but if the calling assembly's property is defined as List<ofSomething> then the PropertyInfo.PropertyType.
Name is "List`1" - not something nice like TheNameOfMyClass.
2. This might not actually be a problem but since I can't solve #1, I have no idea whether this works! I create an instance using Activator.CreateInstance(o
bjType, numItems). Is this correct to initialise a collection to a known number of elements before later calling pinfo.SetValue(item, reference, index)?
Code used to create list:
Type objType = assembly.GetType(assembly.GetName().Name + "." + pinfo.PropertyType.Name, true);
object objInstance = Activator.CreateInstance(objType, numItems);
assembly - Assembly in which to look for type name
pinfo - PropertyInfo of the property
numItems - size of the array/collection
Example class being populated by reflection:
public string TestString;
public int TestInt;
public bool TestBool;
public List<string> TestList;
The string/int/bool work fine (different code from above) but the list doesn't. I can't change the List<string> definition.