在测试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". |
|
来自: aaie_ > 《datasnap》