kayak kayak - 1 month ago 13x
C# Question

Add item to Generic List / Collection using reflection

I want to add an item to a Generic list using reflection. In the method "DoSomething", I am trying to finish the following line,


but I am getting different kinds of error.

Below is my complete code

public class MyBaseClass
public int VechicleId { get; set; }
public class Car:MyBaseClass
public string Make { get; set; }
public class Bike : MyBaseClass
public int CC { get; set; }
public class Main
public string AgencyName { get; set; }
public MyBaseCollection<Car> lstCar {get;set;}

public void DoSomething()
PropertyInfo[] p =this.GetType().GetProperties();
foreach (PropertyInfo pi in p)
if (pi.PropertyType.Name.Contains("MyBaseCollection"))
//Cln contains List<Car>
IEnumerable<MyBaseClass> cln = pi.GetValue(this, null) as IEnumerable<MyBaseClass>;

**//Now using reflection i want to add a new car to my object this.MyBaseCollection**

Any ideas / suggestion ?

Ani Ani

I think you want:

// Cast to IEnumerable<MyBaseClass> isn't helping you, so why bother?
object cln = pi.GetValue(this, null);

// Create myBaseClassInstance. 
// (How will you do this though, if you don't know the element-type?)
MyBaseClass myBaseClassInstance = ...

// Invoke Add method on 'cln', passing 'myBaseClassInstance' as the only argument.
pi.PropertyType.GetMethod("Add").Invoke(cln, new[] { myBaseClassInstance } );

Since you don't know what the element-type of the collection is going to be (could be Car, Bike, Cycle etc.) you're going to find it hard to find a useful cast. For example, although you say the collection will definitely implement IList<SomeMyBaseClassSubType>, that isn't all that helpful since IList<T> isn't covariant. Of course, casting to IEnumerable<MyBaseClass> should succeed, but that won't help you since it doesn't support mutations. On the other hand, if your collection-type implemented the non-generic IList or ICollection types, casting to those might come in handy.

But if you're sure that the collection will implement IList<Car> (i.e. you know the element-type of the collection beforehand), things are easier:

// A much more useful cast.
IList<Car> cln = (IList<Car>)pi.GetValue(this, null);

// Create car.
Car car = ...

// The cast helped!