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

Comments

  1. Rodrigo Ruz That is amazing :D ERTTI is very cool :D

    ReplyDelete
  2. I did some toying with the same, though not just for properties:
    http://paste.ie/view/410c73f1

    ReplyDelete
  3. 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

    I think this is also possible for TRttiIndexedProperty since their getter and setter get typeinfo regardless their visibility.

    ReplyDelete
  4. Stefan Glienke , Indeed the indexed properties are supported. But not the Array properties.

    ReplyDelete
  5. Rodrigo Ruz What do you mean with array properties? A property that returns an array?

    ReplyDelete
  6. Stefan Glienke property Arr[const Index: integer]: string read GetArr;

    At 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.

    ReplyDelete
  7. Ah that'd the be indexed property, gotcha.

    ReplyDelete
  8. Why did they use the word "indexed" for the TRttiIndexProperty when it represents an array property, I wonder...

      for 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;

    ReplyDelete
  9. 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.
    Rodrigo Ruz So you mean properties with an index specifier (http://docwiki.embarcadero.com/RADStudio/Seattle/en/Properties#Index_Specifiers) won't work with RTTI?

    ReplyDelete
  10. 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.

    I edited the blog post to reflect this.

    ReplyDelete

Post a Comment