Delphi 使用内置编组初始化。为什么磁场转换器不工作?

Delphi 使用内置编组初始化。为什么磁场转换器不工作?,delphi,marshalling,delphi-xe,converter,Delphi,Marshalling,Delphi Xe,Converter,我有以下问题: 是否应该将编组放在类帮助器中?或者把它放在课堂的“内部”很好吗 为什么磁场转换器不工作 使用Delphi XE -考虑如下: type TMyClass = Class public FaString : String; FaStringList : TStringList; FMar : TJsonMarshal; procedure RegisterConverters; function Marshal : TJsonObj

我有以下问题:

  • 是否应该将编组放在类帮助器中?或者把它放在课堂的“内部”很好吗
  • 为什么磁场转换器不工作
  • 使用Delphi XE -考虑如下:

    type
      TMyClass = Class
      public
        FaString : String;
        FaStringList : TStringList; 
        FMar : TJsonMarshal;
        procedure RegisterConverters;
        function Marshal : TJsonObject;  // should handle marshalling
      end;
    
    RegisterConverter看起来像这样

    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; 
    
    和封送方法:

    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安装(可能它已损坏)?

    从“内部”执行封送处理时,必须正确设置:-)

    区别在于在对RegisterConverter的调用中使用ClassType作为类型。
    否则就无法到达指定的字段-我应该已经看到了

    我找到了解决办法。。。从“内部”执行封送处理时,必须正确设置:-)
    过程TMyClass.RegisterConverters;开始//尝试抓住封送员本身。FMar.RegisterConverter(ClassType,'FMar',//catch TStringList FMar.RegisterConverter(ClassType,'FaStringList',
    区别在于在调用RegisterConverter时使用ClassType作为类型。否则无法访问指定字段-我应该已经看到了!您可以将其作为答案发布(稍后也可以接受)直到8小时后才能发布-我的代表太低:-)但我会-如果时间限制允许的话。
    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;