C# 使用反射将项添加到常规列表/集合

C# 使用反射将项添加到常规列表/集合,c#,generics,reflection,C#,Generics,Reflection,我想使用反射将项目添加到通用列表中。在“DoSomething”方法中,我试图完成以下一行 pi.PropertyType.GetMethod("Add").Invoke(??????) 但是我得到了不同类型的错误 下面是我的完整代码 public class MyBaseClass { public int VechicleId { get; set; } } public class Car:MyBaseClass { public

我想使用反射将项目添加到通用列表中。在“DoSomething”方法中,我试图完成以下一行

pi.PropertyType.GetMethod("Add").Invoke(??????)
但是我得到了不同类型的错误

下面是我的完整代码

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(??????)
            }
        }    
    }
}
公共类MyBaseClass
{        
公共int向量{get;set;}
}    
公车:MyBaseClass
{
公共字符串Make{get;set;}
}    
公共级自行车:MyBaseClass
{
公共int CC{get;set;}
}        
公共班机
{
公共字符串AgencyName{get;set;}
公共MyBaseCollection lstCar{get;set;}
公共无效剂量测定法()
{
PropertyInfo[]p=this.GetType().GetProperties();
foreach(p中的属性信息pi)
{
if(pi.PropertyType.Name.Contains(“MyBaseCollection”))
{
//Cln包含列表
IEnumerable cln=pi.GetValue(this,null)作为IEnumerable;
**//现在使用反射,我想向我的对象this.MyBaseCollection添加一辆新车**
pi.PropertyType.GetMethod(“添加”).Invoke(???)
}
}    
}
}
有什么想法/建议吗?

typeof.GetMethods
开始,您不调用属性的方法,而是调用属性类型的方法

我想您需要:

// 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 } );

您是否可以一起避免反射并使用:

List<MyBaseClass> lstCar { get; set; }

lstCar.Add((MyBaseClass)new Car());
List lstCar{get;set;}
添加((MyBaseClass)新车());

你也可以考虑使用接口或抽象方法…

< P>作为一种选择…只是不要;考虑非泛型ILIST接口:

IList list = (IList) {... get value ...}
list.Add(newItem);

虽然并非所有泛型集合都必须实现IList,但它们几乎都必须实现IList,因为它支持了如此多的核心框架代码。

MyBaseCollection是什么类型的?它类似于列表吗?并不是所有实现IEnumerable的类都保证有Add方法。@JoeRobich:MyBaseCollection是自己的集合实现,它是从IList派生的,即使是列表的答案也可以解决我的问题……我需要一些通用的方法,上面的代码只是一个示例..在很多Prupose中我都需要它您应该能够通过继承来克服这个问题,我个人认为,如果类和子类的结构正确,则不必使用反射。将项添加到IList肯定会成功,但在我的情况下,我需要将类型转换为IEnumerable bcos。我有一个泛型类型的基类。。“IList cln=(IList)pi.GetValue(this,null);”…因此您的第一个建议得到了解决。。。。(我正在转换为ienumerable以满足其他编码要求)问题:/(如果您不知道元素类型,您将如何执行此操作?)回答:我从对象层次结构中的不同位置获取值…,而不是在运行时创建派生类..上面的代码是stackoverflow prupose的示例..(实时,它更复杂。)我确实提到过这一点,但我认为OP的收藏课是他/她自己的。在这种情况下,不确定是否实现了非通用接口。:)
IList list = (IList) {... get value ...}
list.Add(newItem);