【发布时间】:2011-07-01 18:18:40
【问题描述】:
我正在尝试通过反射调用 ref 类实例上的方法,该方法返回本机指针。
示例引用类头:
public ref class MyRefClass : public IDisposable
{
public:
MyNativeType* GetNativeInstance();
//Rest of the header...
}
这是一个反射尝试失败的例子
void InvokeTheMethod(Object^ obj)
{
MyNativeType* myNative;
GetNativeInvoker^ del = (GetNativeInvoker^) Delegate::CreateDelegate(GetNativeInvoker::typeid, obj, "GetNativeInstance");
//get pointer and use if bind succeeds myNative = del();
//else handle the case where the Object does not have GetNativeInstance()
}
使用这个委托
delegate MyNativeType* GetNativeInvoker();
尝试创建委托时,即使对象是具有方法“GetNativeInstance”(如MyRefClass)的引用类的实例,绑定也会失败并显示ArgumentException。这个问题必须在编译时不知道obj的类型的情况下解决,除了它是Object^。
【问题讨论】:
标签: .net reflection delegates c++-cli