问题描述
我们可以使用SuperObject库通过名称来调用某个对象的方法,并使用SOInvoker方法将其参数作为一个json字符串,就像这样
We can use the SuperObject library to invoke methods of a certain object by its name and giving its parameters as a json string using the SOInvoker method like in this answer
d喜欢知道如何发送创建的对象作为参数。我试图发送它像
I'd like to know how do I send a created object as a parameter. I tried to send it like
LObjectList := TObjectList.Create;
LSuperRttiCtx := TSuperRttiContext.Create;
LSuperObjectParameter := LObjectList.ToJson(LSuperRttiCtx);
SOInvoke(MyInstantiatedObject, 'MyMethod', LSuperObjectParameter);
但在MyMethod内,LObjectList引用丢失。
but inside MyMethod the LObjectList reference is lost.
我做错了什么?
可以下载超级对象库
推荐答案
如果使用对象列表的记录数组,它将起作用。
如果您仍然想使用对象列表,您将必须编写这样的编码器和解码器。我已经为TObjectList编写了编码器/解码器,你必须为对象做同样的事情,并将类名嵌入到某个地方。
It will works if you use array of records intead of object list.If you still want to use object list you will have to write encoders and decoders like this. I have written encoder/decoder for TObjectList, you will have to do the same for your objects and embed the class name somewhere.
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;
这篇关于如何使用SuperObject来调用在Delphi中使用Object作为参数的方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!