我正在编写一个Rtti类,旨在使用Rtti简化和概括操作:
tRTTI_Assistant = class (tObject)
private
fSourceObject : tObject;
...
property SourceObject : tObject read fSourceObject write fSourceObject;
...
end;
如何声明将接收记录的属性?
答案 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)
我不确定你想要做什么。但我认为泛型是你正在寻找的。 p>
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;