从指针获取值到记录类型rtti字段

时间:2013-02-20 12:32:43

标签: delphi delphi-xe2 rtti delphi-xe3

我在使用Delphi的RTTI尝试访问记录数据中记录类型的指针时遇到了麻烦。

请检查我一直在处理的示例代码。

  // Dummy Header
  typDummyHeader = ^tysDummyHeader;
  tysDummyHeader = record
    MessageCode : Integer;
    MessageLength : Integer;
  end;

  // Dummy record having header and trailer
  tysDummyRecord = record
    Header : tysDummyHeader;
    BotAmount : Double;
    SoldAmount : Double;
    SoldQty : Int64;
    BotQty : Int64;
    Tailer : typDummyHeader; // pointer to Dummy Header
  end;

  TclDummy = class
    class function GetFieldValue<T>(const pipInstance : Pointer;
                                const piclField : TRttiField) : string;

    class function ParseAndReturnString<T>(piclObject : T) : string;
  end;

  var
    frmRTTITest: TfrmRTTITest;

  implementation

  {$R *.dfm}

  procedure TfrmRTTITest.FormCreate(Sender: TObject);
  var
    losDummyRecord : tysDummyRecord;
  begin
    FillChar(losDummyRecord, SizeOf(tysDummyRecord), #0);
    losDummyRecord.Header.MessageCode := 5000;
    losDummyRecord.Header.MessageLength := 54433;
    losDummyRecord.BotAmount := 19.45;
    losDummyRecord.SoldAmount := 34.22;
    losDummyRecord.SoldQty := 102;
    losDummyRecord.BotQty := 334;
    losDummyRecord.Tailer := @losDummyRecord.Header;

    ShowMessage(TclDummy.ParseAndReturnString<tysDummyRecord>(losDummyRecord));
  end;

  class function TclDummy.GetFieldValue<T>(const pipInstance : Pointer;
                               const piclField : TRttiField) : string;
  begin
    case piclField.FieldType.TypeKind of
      tkFloat: Result := FloatToStr(piclField.GetValue(pipInstance).AsExtended);
      tkInt64: Result := IntToStr(piclField.GetValue(pipInstance).AsInt64);
      tkInteger: Result := IntToStr(piclField.GetValue(pipInstance).AsInteger);
      tkString: Result := Trim(piclField.GetValue(pipInstance).AsString);
    end;
  end;

  class function TclDummy.ParseAndReturnString<T>(piclObject : T) : string;
  var
    losContext : TRttiContext;
    losContextType : TRttiType;
    loclField : TRttiField;
    losRecordRTTI : TRttiRecordType;
    loclRecordField : TRttiField;
    losPointerType : TRttiPointerType;
    losValue : TValue;
  begin
    Result := EmptyStr;
    losContext := TRttiContext.Create;
    losContextType := losContext.GetType(TypeInfo(T));

    if losContextType.TypeKind = tkRecord then
    begin
      for loclField in losContextType.GetFields do
      begin
        case loclField.FieldType.TypeKind of
          tkRecord:
          begin
            losRecordRTTI := loclField.FieldType.AsRecord;
            for loclRecordField in losRecordRTTI.GetFields do
            begin
              Result := Result + '|' + GetFieldValue<T>(Addr(piclObject), loclRecordField);
            end;
          end; // tkRecord
          tkPointer:
          begin
            losPointerType := loclField.FieldType as TRttiPointerType;

            // Check only record type pointers.
            if losPointerType.ReferredType.TypeKind = tkRecord then
            begin
              losValue := loclField.GetValue(Addr(piclObject));
              if (not losValue.IsEmpty) then
              begin
                for loclRecordField in losPointerType.ReferredType.GetFields do
                begin
                  // Result := Result + '|' + ???????????
                end;
              end;
            end;
          end; // tkPointer
          else
            Result := Result + '|' + GetFieldValue<T>(Addr(piclObject), loclField);
        end;
      end;
    end;
    losContext.Free;
  end;

在上面的示例中,当字段为tkPointer且指向记录类型时,如何从中读取值?

2 个答案:

答案 0 :(得分:1)

Result := Result + '|' + GetFieldValue<T>(Addr(piclObject),loclRecordField);

应该做的。

答案 1 :(得分:1)

抱歉我的英语不好。

@bummi的答案有效,但不对。

这取决于使用情况。

如果你使用下一个代码一切正常:

var
  losDummyRecord : tysDummyRecord;
begin
  FillChar(losDummyRecord, SizeOf(tysDummyRecord), #0);
  losDummyRecord.Header.MessageCode := 5000;
  losDummyRecord.Header.MessageLength := 54433;
  losDummyRecord.BotAmount := 19.45;
  losDummyRecord.SoldAmount := 34.22;
  losDummyRecord.SoldQty := 102;
  losDummyRecord.BotQty := 334;
  losDummyRecord.Tailer := @losDummyRecord.Header;

  ShowMessage(TclDummy.ParseAndReturnString<tysDummyRecord>(losDummyRecord));

但是如果您使用例如此代码,则解析无法正常工作:

var
  losDummyRecord : tysDummyRecord;
  ExternalHeaderVar: tysDummyHeader;
begin
  ExternalHeaderVar.MessageCode := 23;
  ExternalHeaderVar.MessageLength := 25;

  FillChar(losDummyRecord, SizeOf(tysDummyRecord), #0);
  losDummyRecord.Header.MessageCode := 5000;
  losDummyRecord.Header.MessageLength := 54433;
  losDummyRecord.BotAmount := 19.45;
  losDummyRecord.SoldAmount := 34.22;
  losDummyRecord.SoldQty := 102;
  losDummyRecord.BotQty := 334;
  losDummyRecord.Tailer := @ExternalHeaderVar;

  ShowMessage(TclDummy.ParseAndReturnString<tysDummyRecord>(losDummyRecord));

解决方案很简单,可以很好地适用于这两种情况并防止将来使用中出现意外错误:

      tkPointer:
      begin
        losPointerType := loclField.FieldType as TRttiPointerType;

        // Check only record type pointers.
        if losPointerType.ReferredType.TypeKind = tkRecord then
        begin
          losValue := loclField.GetValue(Addr(piclObject));
          if (not losValue.IsEmpty) then
          begin
            losValue.ExtractRawDataNoCopy(@NativeIntVar);
            for loclRecordField in losPointerType.ReferredType.GetFields do
            begin
              Result := Result + '|' + GetFieldValue<T>(Pointer(NativeIntVar),loclRecordField);
            end;
          end;
        end;
      end; // tkPointer