I want to add an item to a Generic list using reflection. In the method “DoSomething”, I am trying to finish the following line,
pi.PropertyType.GetMethod("Add").Invoke(??????)
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**
pi.PropertyType.GetMethod("Add").Invoke(??????)
}
}
}
}
Any ideas / suggestion ?
I think you want:
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 sinceIList<T>isn’t covariant. Of course, casting toIEnumerable<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-genericIListorICollectiontypes, 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: