自从昨天以来,我一直在解决一个问题,但我还没有弄明白...
我有一个类,其中有许多方法,我决定在运行时调用哪个方法。每个方法都返回一个包含我的业务对象元素的列表。
我的类看起来像这样:
public class ReflectiveClass {
public List<BO1> DoSomethingWithBO1(int param){
List<BO1> list = new List<BO1>();
//....
return list;
}
public List<BO2> DoSomethingWithBO2(int param){
List<BO2> list = new List<BO2>();
//....
return list;
}
public void Process(){
//...get MethodInfo and so on
List<object> myReturnValue = (List<object>)methodInfo.Invoke(this, new object[]{param});
// here comes the Exception
}
}
因此,在调用该方法时,我遇到了一个 InvalidCastException 错误,并且调试器告诉我无法进行类型转换。
System.Collections.Generic.List`1[BO1]
为了
System.Collections.Generic.List`1[System.Object]
我想知道为什么这个不起作用。我认为如果使用List,每个对象都可以在这个List中。
我甚至尝试了使用List,但结果相同。
是否有可能反射读取方法返回值的类型?然后我能否使用此返回值创建一个泛型列表并将其转换为此列表?这将非常好。
问候和感谢您的帮助! Benni