分享

internal: cannot instantiate type错误

 aaie_ 2014-01-19
在测试XE2下DataSnap中的 TDBXCallBack回调时,刚开始使用
function Execute(Arg: TObject): TObject; overload; override; 重载方法,传递一个自定义的类信息
然后服务端调用回调时一直报错internal: cannot instantiate type ""... 
 
刚开始以为是XE2传类做为参数有Bug呢,后来改用
function Execute(const Arg: TJSONValue): TJSONValue; overload; override;
把类序列化成JsonValue然后再回传到到客户端,客户端再Unmarshal还原回原数据类
但是问题依旧 ,在网上找到了好些个解决方法无果,
 
研究了半天,最终发现原来是个很低级的错误,
原来是因为 客户端和服务端自定义的类是分别定义的,单元名称不同, 故导致回调时出错
后来,把这个类定义统一放在同名的单元下,就OK了
 
猜想,应该是XE2中的RTTI信息中,使用单元名+类名 做为类的标识的
 
 
附在网上搜到的方法,(虽然以下方法测试无效,但还是备份以供参考)
1- You can use a dummy class method in your class; for example:

TDummyClass = class
public
SomeField : Integer;
class procedure Dummy;
end;

initialization
TDummyClass.Dummy;

2- You can use TypeInfo in the initialization section; for example:

initialization
if TypeInfo(TDummyClass) <> nil then ;

3- Or i think the best way, you can use {$STRONGLINKTYPES ON} compiler directive in your objects unit, for example:

unit MyDummyUnit;
{$STRONGLINKTYPES ON}

type
TDummyClass = class
public
SomeField : Integer;
class procedure Dummy;
end;

After this ways your object will be known by RTTI and you cant give any "cannot instantiate object error".
 

 

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多