如何使用SuperObject调用在Delphi中使用Object作为参数的方法?
发布时间:2020-12-15 10:06:48 所属栏目:大数据 来源:网络整理
导读:我们可以使用SuperObject库通过名称调用某个对象的方法,并使用SOInvoker方法将其参数作为json字符串,如此 answer 我想知道如何将创建的对象作为参数发送.我试着像发送它一样 LObjectList := TObjectList.Create;LSuperRttiCtx := TSuperRttiContext.Create;L
我们可以使用SuperObject库通过名称调用某个对象的方法,并使用SOInvoker方法将其参数作为json字符串,如此
answer
我想知道如何将创建的对象作为参数发送.我试着像发送它一样 LObjectList := TObjectList.Create; LSuperRttiCtx := TSuperRttiContext.Create; LSuperObjectParameter := LObjectList.ToJson(LSuperRttiCtx); SOInvoke(MyInstantiatedObject,'MyMethod',LSuperObjectParameter); 但在MyMethod内部,LObjectList引用丢失了. 我究竟做错了什么? superobject库可以下载到here 解决方法
如果使用对象列表的记录数组,它将起作用.
如果您仍想使用对象列表,则必须编写这样的编码器和解码器.我为TObjectList编写了编码器/解码器,你必须为你的对象做同样的事情并在某处嵌入类名. ctx.SerialToJson.Add(TypeInfo(TObjectList),ObjectListToJSON); ctx.SerialFromJson.Add(TypeInfo(TObjectList),JSONToObjectList); function ObjectListToJSON(ctx: TSuperRttiContext; var value: TValue; const index: ISuperObject): ISuperObject; var list: TObjectList; i: Integer; begin list := TObjectList(value.AsObject); if list <> nil then begin Result := TSuperObject.Create(stArray); for i := 0 to list.Count - 1 do Result.AsArray.Add(encodeyourobject(list[i])); end else Result := nil; end; function JSONToObjectList(ctx: TSuperRttiContext; const obj: ISuperObject; var Value: TValue): Boolean; var list: TObjectList; i: Integer; begin list := nil; case ObjectGetType(obj) of stNull: begin Value := nil; Result := True; end; stArray: begin list := TObjectList.Create; for i := 0 to obj.AsArray.Length - 1 do list.Add(decodeyourobject(obj.AsArray[i])); Value := list; Result := True; end; else result := False; end; end; (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |