11

我正在尝试使用 Delphi XE 或更高版本中增强的 RTTI 功能来读取和写入 XML 对象。到目前为止,我在整数、浮点数、字符串、枚举类型、集合和类方面取得了成功,但无法正确输出或读取记录。问题似乎是获取一个指向记录属性的实例(指针)。

//Outputs Properties To XML
procedure TMyBase.SaveToXML(node: TJclSimpleXMLElem);
var
  child , subchild : TjclSimpleXMLElem ;
  FContext : TRttiContext ;
  FType    : TRttiType ;
  FProp    : TRttiProperty ;
  Value    : TValue ;
  MyObj    : TMyBase ;
  FField   : TRttiField ;
  FRecord  : TRttiRecordType ;
  Data     : TValue ;
begin
  FContext := TRttiContext.Create ;
  FType := FContext.GetType ( self.ClassType ) ;
  Child := node.Items.Add ( ClassName ) ;
  for FProp in FType.GetProperties do begin
    if FProp.IsWritable then begin
      case FProp.PropertyType.TypeKind of
        tkClass : begin
          MyObj := TMyBase ( FProp.GetValue ( self ).AsObject ) ;
          MyObj.SaveClass ( Child.Items.Add ( FProp.Name ) , FContext ) ;
          end ;
        tkRecord : begin
          subchild := Child.Items.Add ( FProp.Name ) ;
          FRecord := FContext.GetType(FProp.GetValue(self).TypeInfo).AsRecord ;
          for FField in FRecord.GetFields do begin
            >>> self is not the correct instance <<<
            Value := FField.GetValue ( self ) ;
            subchild.Items.Add ( FField.Name ).Value := Value.ToString ;
            end;
          end ;
        else begin
          Value := FProp.GetValue(self) ;
          Child.Items.Add ( FProp.Name ).Value := Value.ToString ;
          end;
        end;
      end ;
    end ;
  FContext.Free ;
end;

我怀疑如果我能弄清楚如何获取这些值,那么设置它们应该不是问题。然后进入数组,哦,天哪!

更新:请见下文。(迁移为单独的答案以提高可见性)。

4

2 回答 2

12

我想您正在尝试保存 Self 运行时类型的记录类型字段的值,是吗?

您必须先获取字段的值,使用FProp.GetValue(Self). 假设你把它放在一个名为FieldValuetype的变量中TValue。然后,您可以根据需要保存记录值的字段,尽管您可能希望为其编写递归过程,因为记录的字段本身可能是字段。记录的字段 getter 期望记录的地址(指向其开始的指针)与 setter 对称;setter 期望的是地址而不是值,否则将没有简单的方法来“原地”修改另一个类或记录中的字段,因为记录是按值传递的。

您可以使用 来获得它FieldValue.GetReferenceToRawData,这将返回一个指向存储在TValue.

希望这能给你足够的线索来继续。

于 2011-01-18T04:45:34.370 回答
5

归属:最初由 OP ( Mitch ) 作为问题更新发布 - 作为单独的答案迁移以提高可见性。

巴里的解决方案成功了。这是修改后的代码:

    tkRecord : begin
      subchild := Child.Items.Add ( FProp.Name ) ;
      Value := FProp.GetValue(self) ;
      FRecord := FContext.GetType(FProp.GetValue(self).TypeInfo).AsRecord ;
      for FField in FRecord.GetFields do begin
        Data := FField.GetValue ( Value.GetReferenceToRawData ) ;
        subchild.Items.Add ( FField.Name ).Value := Data.ToString ;
        end;
      end ;

对于那些需要处理数组的人:

    tkDynArray : begin
      Value := FProp.GetValue ( self ) ;
      FArray := FContext.GetType(Value.TypeInfo) as TRttiDynamicArrayType ;
      subchild := child.Items.Add ( FProp.Name ) ;
      cnt := Value.GetArrayLength ;
      subchild.Properties.Add ( 'Count' , cnt ) ;
      case FArray.ElementType.TypeKind of
        tkInteger ,
        tkFloat   : begin
          for a := 0 to cnt-1 do begin
            Data := Value.GetArrayElement ( a ) ;
            subchild.Items.Add ( IntToStr(a) , Data.ToString ) ;
            end;
          end ;
        tkRecord  : begin
          FRecord := FArray.ElementType as TRttiRecordType ;
          for a := 0 to cnt-1 do begin
            Data := Value.GetArrayElement ( a ) ;
            subsubchild := subchild.Items.Add ( IntToStr(a) ) ;
            for FField in FRecord.GetFields do
              SaveField ( subsubchild , FContext , FField , Data.GetReferenceToRawData ) ;
            end;
          end ;
于 2012-02-26T05:52:47.963 回答