jeudi 4 juin 2015

Instantiate generic c# List when the reflected type itself is a List

I have a reflected Type, which ends up being a List<Type>. So I have:

Type modelType = GetMyType();

So - modelType could be List<ClassA> or List<ClassB>, etc. depending on the situation.

How do I create this type, and then populate it?

I know I can do this:

var myList = Activator.CreateInstance(modelType);

But then how do I add items to it, which I would normally do with myList.Add(new ClassA()) or myList.Add(new ClassB()) knowing that I don't really know the ClassA or ClassB type - I just know that modelType is List<ClassA>





Aucun commentaire:

Enregistrer un commentaire