Your code contains two incorrect assumptions:
So you can get meaningful RTTI from the interfaces. Alas, you can get RTTI from interface types.- The interface is always implemented by the Delphi object (therefore, your attempt to extract the RTTI from the Delphi object).
Both assumptions are wrong. Interfaces are very simple tables of VIRTUAL METHODS, very little magic for them. Since the interface is so narrowly defined, it cannot have RTTI . Unless, of course, you are implementing your own version of RTTI , and you shouldn't. LE: the interface itself cannot carry type information as TObject does, but the TypeOf () operator can get TypeInfo if equipped with IInterface
Your second assumption is also incorrect, but even more so. In the Delphi world, most interfaces will be implemented by Delphi objects, unless, of course, you get an interface from a DLL written in another programming language: Delphi interfaces are COM-compatible, so implementations can be used from any other COM-compatible language and vice versa. But since we are talking about Delphi XE here, you can use this syntax to cast an interface to it that implements the object in an intuitive and understandable way:
TObject := IInterface as TObject;
i.e. use the as operator. Delphi XE automatically converts a hard listing of this type from time to time:
TObject := TObject(IInterface);
to the "as" syntax mentioned, but I don't like this magic because it looks very contrast-intuitive and behaves differently in older versions of Delphi.
Bringing an Interface back to it, which implements the object, is also wrong from another point of view: it displays all the properties of the implementation object, and not just those that are associated with the interface, and this is very wrong because you use Interfaces to hide these implementation details Firstly!
Example: interface implementation not supported by Delphi object
Just for fun, here's a quick demo of an interface not supported by a Delphi object. Since the interface is nothing more than a pointer to a table of virtual methods, I will build a table of virtual methods, create a pointer to it and draw a pointer to the desired type of interface. All method pointers in my fake virtual method table are implemented using global functions and procedures. Imagine trying to extract RTTI from my i2 interface!
program Project26; {$APPTYPE CONSOLE} uses SysUtils; type