如何使用Rtti获取Record的详细信息?

时间:2014-11-06 13:38:31

标签: delphi delphi-xe rtti

我正在编写一个Rtti类,旨在使用Rtti简化和概括操作:

tRTTI_Assistant = class (tObject)
 private
  fSourceObject : tObject;
 ...
  property SourceObject : tObject read fSourceObject write fSourceObject;
 ...
 end;

如何声明将接收记录的属性?

2 个答案:

答案 0 :(得分:0)

您无法添加可以采用任何记录结构的记录类属性,并使用RTTI来解析记录的内部详细信息。

为此,你必须采用另一种方式。 TValue可与RTTI一起使用以解析任何类型。

声明:

tRTTI_Assistant = class (tObject)
 private
  //fSourceObject : tObject; // ! Use AnySource for objects as well
  fAnySource : TValue;
 ...
  //property SourceObject : tObject read fSourceObject write fSourceObject;
  property AnySource: TValue read fAnySource write fAnySource;
 ...
 end;

并称之为:

myAssistant.AnySource := TValue.From(myRecord);

现在,您可以使用RTTI解析记录类型,但不仅可以解析任何类型。

有关如何在TValue上使用RTTI的示例,请参阅Convert Record to Serialized Form Data for sending via HTTP

答案 1 :(得分:-1)

我不确定你想要做什么。但我认为泛型是你正在寻找的。

type
  TMyRecord = record
    I:Integer;
    S:string;
    B:Boolean;
  end;

  TMyObject<T:record> = class
  private
    FMyRecord:T;
  public
    property MyRecord: T read FMyRecord write FMyRecord;
  end;
相关问题