delphi - 具有内置编码的类。为什么现场转换器不工作?

标签 delphi marshalling delphi-xe converter

我有以下问题:

  • 编码应该放在类助手中吗?或者将它放在类“内部”是否完全可以?
  • 为什么现场转换器不工作?

  • 使用 Delphi XE - 考虑一下:
    type
      TMyClass = Class
      public
        FaString : String;
        FaStringList : TStringList; 
        FMar : TJsonMarshal;
        procedure RegisterConverters;
        function Marshal : TJsonObject;  // should handle marshalling
      end;
    

    RegisterConverters 看起来像这样。
    procedure TMyClass.RegisterConverters;
    begin
      // try and catch the marshaller itself.
      FMar.RegisterConverter(TJsonMarshal, 'FMar',
        function(Data : TObject; Field:String): TObject
        begin
          Exit(nil); // Since we cannot marshal it - and we dont need it anyways.
        end);
      // catch TStringList
      FMar.RegisterConverter(TStringList, 'FaStringList',
        function(Data: TObject; Field:String): TListOfStrings
        var
          i, count: integer;
        begin
          count := TStringList(Data).count;
          SetLength(Result, count);
          for i := 0 to count - 1 do
            Result[i] := TStringList(Data)[i];
        end);
    end; 
    

    和编码(marshal)方法:
    function TMyClass.Marshal: TJSONObject;
    begin
      if FMar = nil then
        FMar := TJSONMarshal.Create(TJSONConverter.Create);
      try
        RegisterConverters;
        try
          Result := FMar.Marshal(Self) as TJSONObject;
        except
          Result := nil;
        end;
      finally
        FMar.Free;
      end;
    end;
    

    然后我们可以这样做:
    var
      aObj : TMyClass;
      ResultString : String;
    begin
      aObj := TMyClass.Create;
      aObj.FaString := 'Test string';
      aObj.FaStringList := TStringList.Create;
      aObj.FaStringList.Add('stringliststring #1');
      aObj.FaStringList.Add('stringliststring #2');
      aObj.FaStringList.Add('stringliststring #3');
      aObj.FaStringList.Add('stringliststring #4');
    
      // StringList and JsonMarshal should be handled by converter
      ResultString := (aObj.Marshal).ToString;
    end;
    

    但我根本无法让它工作。场转换器不会被触发?

    我在这里做错了吗?或者我应该看看我的 Delphi XE 安装(也许它是被骗的)?

    最佳答案

    从“内部”执行编码(marshal)时,您必须正确设置它:-)

    procedure TMyClass.RegisterConverters;
    begin
      // try and catch the marshaller itself.
      FMar.RegisterConverter(ClassType, 'FMar',
        function(Data : TObject; Field:String): TObject
        begin
          Exit(nil); // Since we cannot marshal it - and we dont need it anyways.
        end);
      // catch TStringList
      FMar.RegisterConverter(ClassType, 'FaStringList',
        function(Data: TObject; Field:String): TListOfStrings
        var
          i, count: integer;
        begin
          count := TStringList(Data).count;
          SetLength(Result, count);
          for i := 0 to count - 1 do
            Result[i] := TStringList(Data)[i];
        end);
    end; 
    

    不同之处在于使用 ClassType 作为对 RegisterConverter 的调用中的类型。
    否则无法到达指定的字段 - 我应该看到的!

    关于delphi - 具有内置编码的类。为什么现场转换器不工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7608415/

    相关文章:

    delphi - 字符串和整数访问和保存

    delphi - 没有无参数构造函数的泛型

    delphi - Delphi 2009 应用程序中的 Google map

    json - 在 Go 中将 JSON 解码为自定义格式

    amazon-s3 - 发出 S3 GET 请求时收到 400 Bad request

    Delphi:从继承的表单中删除可视组件引用

    java - 有条件地将元素渲染为链接以在 Java 中实现 HAL

    c# - 将 C# 引用传递给 C++ 指针并返回

    delphi - Delphi卡住窗体并关闭自定义组件

    delphi - Delphi的属性语言特性可以注释哪些语言元素?