TRttiIndexedProperty无法被移植到旧的Delphi版本,因为它取决于编译器为索引属性写出RTTI数据,只有Delphi XE2的编译器所做的工作。你无法阅读那些不存在的东西。
您拥有的唯一可能性就是手工编写这些数据。所以你必须编写一个解析器来运行你的所有代码,并为所有索引属性生成必要的类型信息。因为你的解析器不是编译器,所以你也必须编写一些帮助函数来编写和读取索引属性。 输出可能是这样的:
TMyClass = class
private
...
public
property MyArray[Index: Integer]: TMyObject read GetMyArray write SetMyArray;
// autogenerated code
class procedure RegisterIndexedPropertyInfos(Registry: TMyRttiIndexedPropertyRegistry); static;
end;
// autogenerated code
class procedure TMyClass.RegisterIndexedPropertyInfos(Registry: TMyRttiIndexedPropertyRegistry): TMyRttiIndexedProperty;
begin
Registry.Register('MyArray', [TMyRttiIndex.Create('Index', TypeInfo(Integer))], TypeInfo(TMyObject), @TMyClass.GetMyArray, @TMyClass.SetMyArray);
end;
// When using RichRTTI you can omit this line and use the the RttiContext to find RegisterIndexedPropertyInfos
RegisterIndexedPropertyClass(TMyClass, @TMyClass.RegisterIndexedPropertyInfos);
这个问题与洁净室实现有什么关系? – 2012-03-13 20:17:56
@Rob现在它是一个答案。 – 2012-03-13 20:52:34
@Rob Kennedy:标题改为。 – menjaraz 2012-03-14 04:20:34