基于以下类似的代码:
TListWrapper = class
strict private
FList: TStringList;
function GetItem(index: Integer): TObject; overload;
function GetItem(index: string): TObject; overload;
public
property Items[index: Integer]: TObject read GetItem; default;
property Items[index: string]: TObject read GetItem; default;
end;
我想编写一段代码,使用RTTI获取字符串索引属性的值。类似于这样:
var
MyList: TListWrapper;
InstanceType: TRttiInstanceType;
IndexedProperty: TRttiIndexedProperty;
begin
MyList:=TListWrapper.Create;
LContext:=TRttiContext.Create;
InstanceType:=LContext.GetType(MyList.ClassType) as TRttiInstanceType;
for IndexedProperty in InstanceType.GetIndexedProperties do
if IndexedProperty.Name.ToLower = 'items' then
begin
//There are two indexed properties with name 'items'
end;
LContext.Free;
MyList.Free;
end;
问题:我如何知道哪个索引属性具有字符串索引,以便我可以像这样获取值?
IndexedProperty.GetValue(MyList, ['some_string_index']);
注意:我正在使用Delphi 10.2.3(东京版)。
string
类型是UnicodeString
,因此在这个例子中,TypeKind
将会是tkUString
(tkString
代表ShortString
,tkLString
代表AnsiString
,tkWString
代表WideString
)。 - Remy Lebeau