Based on some code like the following:
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;
I want to write a piece of code which get the value of the string indexed property using RTTI. Something like this:
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;
Question: How can I know which of the indexed property has the string index so that I can then get the value like this?
IndexedProperty.GetValue(MyList, ['some_string_index']);
Note: I am using Delphi 10.2.3 (Tokyo)
You should be able to use the read method parameters. Something like this:
readingMethod := IndexedProperty.ReadMethod;
readMethodParameters := readingMethod.GetParameters;
if readMethodParameters[0].ParamType.TypeKind = tkUString then
// We have the string version
You should obviously check that the readMethod is assigned and that the number of parameters is greater than zero, etc.
From Remy:
In this case, the string type is tkUString (UnicodeString) and it has been this way since Delphi 2009.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.