Delphi中如何使用SuperObject调用使用Object作为参数的方法?

发布于 2024-12-11 11:26:07 字数 651 浏览 3 评论 0原文

我们可以使用 SuperObject 库通过名称调用某个对象的方法,并使用 SOInvoker 方法将其参数作为 json 字符串,如下所示 answer

我想知道如何发送创建的对象作为参数。我尝试发送它,

LObjectList := TObjectList.Create;
LSuperRttiCtx := TSuperRttiContext.Create;
LSuperObjectParameter := LObjectList.ToJson(LSuperRttiCtx);

SOInvoke(MyInstantiatedObject, 'MyMethod', LSuperObjectParameter);

但在 MyMethod 中,LObjectList 引用丢失了。

我做错了什么?

可以在此处下载超级对象库

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

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);

but inside MyMethod the LObjectList reference is lost.

What am I doing wrong?

The superobject library can be downloaded here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

も让我眼熟你 2024-12-18 11:26:07

如果您使用记录数组而不是对象列表,它将起作用。
如果您仍然想使用对象列表,则必须像这样编写编码器和解码器。我已经为 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;

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;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文