我想知道如何使用C++Builder 2010中TRttiMethod类的Invoke方法。
这是我的代码
Tpp *instance=new Tpp(this);
TValue *args;
TRttiContext * ctx=new TRttiContext();
TRttiType * t = ctx->GetType(FindClass(instance->ClassName()));
TRttiMethod *m=t->GetMethod("Show");
m->Invoke(instance,args,0);
Show没有参数,并且它是__published。当我执行时,我得到了一个EInvocationError消息‘参数计数不匹配’。
有人能演示一下Invoke的用法吗?在被调用的方法中既没有参数又有参数。
谢谢
何塞普
发布于 2010-07-07 08:38:42
之所以会出现这个错误,是因为您告诉Invoke()您传入了一个方法参数(即使您确实没有这样做,但这是代码中的一个单独的bug )。Invoke()接受TValue值的OPENARRAY
作为输入。尽管名为Args_Size
参数,但它不是传入的参数数量,而是数组中最后一个参数的索引。因此,要通过Invoke()将0个方法参数传递给Show(),请将Args
参数设置为NULL,并将Args_Size
参数设置为-1而不是0,即:
Tpp *instance = new Tpp(this);
TRttiContext *ctx = new TRttiContext;
TRttiType *t = ctx->GetType(instance->ClassType());
TRttiMethod *m = t->GetMethod("Show");
m->Invoke(instance, NULL, -1);
delete ctx;
现在,一旦修复了这个问题,您将注意到Invoke()开始引发EInsufficientRtti异常。当运行时包被启用时,就会发生这种情况。不幸的是,禁用运行时包将导致TRttiContext::GetType()在TRttiPool::GetPackageFor()中引发EAccessViolation,因为C++:
QC #76875, RAID #272782: InitContext.PackageTypeInfo shouldn't be 0 in a C++ module
这会导致这些错误:
QC #76672, RAID #272419: Rtti.pas is unusable in a C++ application
QC #76877, RAID #272767: AV in TRttiContext::GetType() when Runtime Packages are disabled
所以你正处于一种进退两难的境地。新的RTTI系统还没有为C++的生产工作做好准备。你将不得不暂时使用Delphi来代替。
https://stackoverflow.com/questions/3113379
复制相似问题