I’m so glad I just bumped into the below quote of at Delphi sorcery: CodeRage 6 feedback and DSharp news:
He put an entire class into the implementation part of a unit registering it to the container in the initialization part of the unit. I mentioned the dependency on the container in that case and tried something else: using TRttiContext.GetTypes to iterate all known types looking for attributes that tell to register that type. But it did not list the class in the implementation part of the unit. The class was not thrown out by the smart linker because it was actually used. It just seems to be the case that GetTypes does not find any types in the implementation part while GetType(…) works.
I bumped into this for interface and class types a while ago when writing demos for the upcoming Coding In Delphi book by Nick Hodges.
Part of the demo is RttiHelper.pas, which in part consists of record helpers that extend TRttiContext.
TRttiContext has a few methods, of which FindType, the GetType overloads and GetTypes are the most interesting ones. At first glance, you’d think the get* methods would return results in the same way: GetTypes just returns all the types that GetType could return. But that is not true. GetTypes should have been named after FindType.
FindType already gives you a hint in its parameter `AQualifiedName: string` and the decription:
FindType searches for the type in all packages and works only for public types that have qualified names.
Private types (types in the implementation section of units) do not get qualified names.
In fact, then you call the method TRttiType.QualifiedName on a type that is in the interface section (private) it won’t find any, and return an error like this::
ENonPublicType: Type ‘IPrivateImplementedInterface’ is not declared in the interface section of a unit.
So I introduced TRttiTypeHelper and named all methods that depend on GetTypes with the name FindType or FindTypes:
Two notes here:
- There are two other exceptions Rtti can get you: EInsufficientRtti and EInvocationError. I won’t go into detail in this posting, maybe in a later one.
- System.TObject.QualifiedClassName does not suffer from the limitation: it just shows you the unit name for both private and public types.
The second note came to me as a big surprise: the RTTI infrastructure things differently about qualified names than TObject!
To demonstrate this, I’ve written the unit RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit:
Together with RttiContext_GetTypes_vs_GetType_on_Interfaces_ConsoleReportUnit it will return the output at the end of the post of which I’ll show two excertps
You see the really odd lines there:
Found GUID "{E1962EFF-64CD-4ABB-9C44-1503E3CE77A9}" with best private name "IPrivateImplementedInterface"
Found GUID "{AD72354C-BD8D-4453-8838-A9C261115A25}" with best private name "IPrivateNonImplementedInterface"
private "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.TPrivateImplementingObject" with RTTI name "TPrivateImplementingObject"
private "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.TPrivateImplementingObjectWithoutTypeInfoCalls" with RTTI name "TPrivateImplementingObjectWithoutTypeInfoCalls
There you clearly see the difference between the two ways of getting a qualified name. Very confusing!
The other amazing thing is that GetTypes does not work for anything private in the implementation section:
public best name "RttiHelpers.TRttiTypeHelper" with QualifiedClassName "RttiHelpers.TRttiTypeHelper"
public best name "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.IPublicImplementedInterface"
public best name "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.IPublicNonImplementedInterface"
public best name "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.TPublicImplementingObject" with QualifiedClassName "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.TPublicImplementingObject"
public best name "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.IPublicImplementedInterfaceThatHasNoTypeInfoCalls"
public best name "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.TPublicImplementingObjectWithoutTypeInfoCalls" with QualifiedClassName "RttiContext_GetTypes_vs_GetType_on_Interfaces_MainUnit.TPublicImplementingObjectWithoutTypeInfoCalls"
–jeroen
via: Delphi sorcery: CodeRage 6 feedback and DSharp news.
Filed under: Delphi, Delphi 2010, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Development, Software Development