【发布时间】:2015-10-24 03:18:12
【问题描述】:
我试图获取一个函数列表,而获取列表的变量是一个对象。 我正在尝试为函数创建一个通用选项,无需为特定列表设置函数。
class Program
{
static void Main(string[] args)
{
Car Ford = new Car();
Car Toyota = new Car();
Motorcycle Kawasaki = new Motorcycle();
Bike Zebra = new Bike();
List<Vehicle> Garage = new List<Vehicle>();
Garage.Add(Ford);
Garage.Add(Toyota);
Garage.Add(Kawasaki);
Garage.Add(Zebra);
Class1 c1 = new Class1(Garage);
}
}
class Class1
{
public Class1(object obj)
{
//cannot find the way to extract the list and its method
}
}
我可以在 obj 中看到列表,但我无法使用它(无法循环类并且不使用它们的公共方法。 我知道有一些使用反射的可能性,但我只能使用一个类而不是一个列表。 这里有什么帮助吗?
【问题讨论】:
-
我不明白?只需将构造函数中的对象替换为 list
并将garage 作为参数传递? -
我不想写列表
因为我希望对象能够获取,并且我将使用反射来使用方法 -
那么你说的是哪个对象?
-
class Class1 { public Class1(object obj) { //找不到提取列表的方法及其方法 } }
-
@YDG 你想实际做什么?这是错误的做法。
标签: c#