Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Use of TRttiContext



Permlink Replies: 1 - Last Post: Apr 16, 2015 2:04 PM Last Post By: Remy Lebeau (Te...
Hector Laguna

Posts: 1
Registered: 6/28/13
Use of TRttiContext
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 16, 2015 7:58 AM
I have the following class from which I would like to obtain the names of the members:

TInterface = interface(IXMLNode)
function Get_One: Boolean;
function Get_Two: Boolean;
function Get_Three: Boolean;
procedure Set_One(Value: Boolean);
procedure Set_Two(Value: Boolean);
procedure Set_Three(Value: Boolean);
property One: Boolean read Get_One write Set_One;
property Two: Boolean read Get_Two write Set_Two;
property Three: Boolean read Get_Three write Set_Three;
end;

TTesting = class(TXMLNode, TInterface)
protected
function Get_One: Boolean;
function Get_Two: Boolean;
function Get_Three: Boolean;
procedure Set_One(Value: Boolean);
procedure Set_Two(Value: Boolean);
procedure Set_Three(Value: Boolean);
end;

I wrote this method, expecting to get those names:

procedure getNames();
var
Ctx: TRttiContext;
PropList: TArray<TRttiProperty>;
begin
PropList := Ctx.GetType(TTesting).GetProperties;

for i:= 0 to Length(PropList) do S1:= PropList[i].name; PropList[i].ToString

However I always got 1 item called RefCount.

Should not I get 'One' 'Two' 'Three' ?

Help, thank you

Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Use of TRttiContext
Click to report abuse...   Click to reply to this thread Reply
  Posted: Apr 16, 2015 2:04 PM   in response to: Hector Laguna in response to: Hector Laguna
Hector wrote:

Should not I get 'One' 'Two' 'Three' ?

No. Interfaces are a special case. An interface is only allowed to have
methods, properties are just syntax sugar to call the methods. So properties
defined on an interface are not real properties, and thus do not generate
RTTI. That is why you cannot enumerate properties being inherited from an
interface. You can enumerate the interface's methods with Extended RTTI,
but only if the interface has been marked with {$M+}.

BTW, I updated my answer to this same qustion on StackOverflow:

List all the properties from a class that implements an interface
http://stackoverflow.com/questions/29525127/

--
Remy Lebeau (TeamB)
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02