我想调用一个返回 a的方法struct
,使用该MethodInfo.Invoke
方法。但是,此方法的返回变量的类型是object
,不能强制转换为struct
。
(发现,以及从切换struct
到的建议class
,在这里:http: //msdn.microsoft.com/en-us/library/b7tch9h0 (VS.90).aspx )
那么如何将调用方法的(值)结果转换为正确的类型呢?
(我也尝试过简单的演员阵容,但它抛出了 InvalidCastException)
以下是我的一些代码块:
public class MyInvokedClass
{
public struct MyStruct
{ ... };
public MyStruct InvokedMethod()
{
MyStruct structure;
...
return structure;
}
}
public class MyInvokingClass
{
// Same structure here
public struct MyStruct
{ ... };
static void Main(string[] args)
{
...
MethodInfo methodInfo = classType.GetMethod(methodName);
MyStruct result = (MyStruct)methodInfo.Invoke(classInstance, null);
// the line above throws an InvalidCastException
}
}
谢谢你们!