views:

100

answers:

3

Is there a way to explore a interface's properties with Rtti?

The following code does not work:

procedure ExploreProps;
var
  Ctx: TRttiContext;
  RttiType: TRttiType;
  RttiProp: TRttiProp;
begin
  RttiType := Ctx.GetType(TypeInfo(IMyInterface));
  for RttiProp in RttiType.GetProperties do
    Writeln(RttiProp.ToString);
end;

Has anyone a solution how to do this correctly?

+1  A: 

Interfaces are collections of functions. They don't really have properties the way objects do; that's just a bit of syntactic sugar that the compiler adds for you to make it easier to write code for them. The difference is that on objects, properties allow controlled access to private and protected members, whereas on interfaces, all members are public so there's no need for the properties.

Mason Wheeler
Ok, but exploring a interfaces methods doesn't work either... Just replaced the for loop by using RttiType.GetMethods, still no results.
Christian Metzler
@Christian: I just looked at the code for the RTTI system, and a lot of interfaces in the standard libraries are set up with no RTTI generated for them. I'm not sure what the rules are for generating extended RTTI for interface types, since it seems to be different from generating extended RTTI for classes or records. Maybe Barry Kelly or Allen Bauer could answer this one?
Mason Wheeler
A: 

As I known, there is no support for normal interfaces. You could add {$M+} and then try again.

Paul
A: 

Here is a blog-post with a nice description of what is possible. Hopefully it still works the same in newer versions.

pritaeas