mercredi 27 juillet 2016

C# Creating a List of Generic type using Reflection

I have been trying various combinations of answers I see online but I cannot quite get it working.

I have a PropertyInfo object as follows

Type myObj = something.GetType();
PropertyInfo other = myobj.GetProperty("People");

I then check if that property is a type of List<>

 other.PropertyType.GetInterface(typeof(IList<>).FullName) != null

What I cannot put together is, if this property called "People" is a List<Person> (where Person is some class in my solution), how do use reflection to create this object?

I need the mechanism to be Generic so that I can create an initialised empty list of and object.

I'm not sure what I'm missing to be able to do that. I'm may not be taking the right approach. Essentially, given that I detect a type of List or Enumerable of a given type, I need to return the initialised version of it. I hope this is clear enough for someone to help





Aucun commentaire:

Enregistrer un commentaire