The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,470 other followers

Delphi: TRttiContext.GetType versus TRttiContext.GetTypes is not about singular versus plural, but about all versus public types

Posted by jpluimers on 2014/03/25

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:

  1. 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.
  2. 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.

3 Responses to “Delphi: TRttiContext.GetType versus TRttiContext.GetTypes is not about singular versus plural, but about all versus public types”

  1. Michel Bernabela said

    I have a configurationsetting unit with the objects declaration. On the other hand a have a configurationsettinghandler unit which in turn load and save to the object in configurationsetting. The load function works fine, but when saving the JSON “objecttojsonstring(object) fires the error ENonPublicType: Type ‘Tobject’ is not declared in the interface section of a unit.
    Hope you can help me with this issue… Michel (intellisoft@setarnet.aw)

  2. sglienke said

    First there is a bug in your code. You are using the same GUIDs for the public and private interfaces which will result in wrong output. You search the type by given GUID which will return the public interface which does not match the private interface.

    Then the behaviour actually is the way it is (I learned that since I posted that article) because of some facts:
    Classes always know their unit name because that is actually compiled in (see System.TClassData).

    When using GetTypes it iterates all TPackageTypeInfo tables for each loaded module which just contains a list of all public types.
    When you are using GetType you pass in a type info which can be a type info of a private type. Then it does not need to scan any list but can directly retrieve the RTTI from that type.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

 
%d bloggers like this: