New blog post https://theroadtodelphi.wordpress.com/2015/09/25/getting-the-getter-and-setter-of-a-property-using-rtti/
New blog post https://theroadtodelphi.wordpress.com/2015/09/25/getting-the-getter-and-setter-of-a-property-using-rtti/
https://theroadtodelphi.wordpress.com/2015/09/25/getting-the-getter-and-setter-of-a-property-using-rtti
https://theroadtodelphi.wordpress.com/2015/09/25/getting-the-getter-and-setter-of-a-property-using-rtti
Rodrigo Ruz That is amazing :D ERTTI is very cool :D
ReplyDeleteLike reflection in Java
ReplyDeleteI did some toying with the same, though not just for properties:
ReplyDeletehttp://paste.ie/view/410c73f1
You beat me to it - I had that idea some time go but not the time to implement it ;) So don't be surprised if it appears in Spring4D in the future
ReplyDeleteI think this is also possible for TRttiIndexedProperty since their getter and setter get typeinfo regardless their visibility.
Stefan Glienke , Indeed the indexed properties are supported. But not the Array properties.
ReplyDeleteRodrigo Ruz What do you mean with array properties? A property that returns an array?
ReplyDeleteStefan Glienke property Arr[const Index: integer]: string read GetArr;
ReplyDeleteAt least the following only prints "Index1" and "Index2".
uses
System.SysUtils,
System.Rtti;
type
TWidget = class
private
function GetIndexed(const Index: Integer): integer;
function GetArr(const Index: integer): integer;
public
property Indexed1: integer index 1 read GetIndexed;
property Indexed2: integer index 2 read GetIndexed;
property Arr[const Index: integer]: integer read GetArr;
end;
{ TWidget }
function TWidget.GetArr(const Index: integer): integer;
begin
end;
function TWidget.GetIndexed(const Index: Integer): integer;
begin
end;
var
ctx: TRttiContext;
typ: TRttiType;
p: TRttiProperty;
begin
ctx := TRttiContext.Create.Create;
typ := ctx.GetType(TWidget);
for p in typ.GetProperties() do
begin
WriteLn(p.Name);
end;
ReadLn;
end.
Ah that'd the be indexed property, gotcha.
ReplyDeleteWhy did they use the word "indexed" for the TRttiIndexProperty when it represents an array property, I wonder...
ReplyDeletefor p in typ.GetProperties() do
begin
WriteLn('Property: ', p.Name); // Index1 and Index2
end;
for ip in typ.GetIndexedProperties() do
begin
WriteLn('Indexed property: ', ip.Name); // Arr
end;
This is another case of not properly naming things, people call them array properties or indexed properties, because even if they look like an array access there is no array involved in these properties. The funny thing about these properties and those with the index specifier is they both have the same getter signature.
ReplyDeleteRodrigo Ruz So you mean properties with an index specifier (http://docwiki.embarcadero.com/RADStudio/Seattle/en/Properties#Index_Specifiers) won't work with RTTI?
Stefan Glienke I was talking about the Array properties http://docwiki.embarcadero.com/RADStudio/Seattle/en/Properties#Array_Properties , those doesn't are listed by the GetProperties method. I just checked which the Arrays properties are listed using the GetIndexedProperties method and the TRttiIndexedProperty already includes the ReadMethod and WriteMethod to return an instance to the TRttiMethod for the getters ans setters.
ReplyDeleteI edited the blog post to reflect this.