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,999 other followers

Archive for March 25th, 2014

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"


via: Delphi sorcery: CodeRage 6 feedback and DSharp news.

Posted in Delphi, Delphi 2010, Delphi XE, Delphi XE2, Delphi XE3, Delphi XE4, Delphi XE5, Development, Software Development | 3 Comments »

Traffic (public, car) sites I watched during the #NSS2014: not many congestions yet

Posted by jpluimers on 2014/03/25

And for future big events, here are some sites to watch to see Dutch traffic congestion:


Posted in LifeHacker, Power User | Leave a Comment »

Hydrogen in 60 seconds | Hydrogen

Posted by jpluimers on 2014/03/25

A different Hydrogen: Hydrogen in 60 seconds | Hydrogen.


via: Hydrogen | advanced drum machine for GNU/Linux (as well as Mac OS X and Windows).

Posted in LifeHacker, Power User | Leave a Comment »

When opening SQL Server Management studio with ssms command (+parameters) I need the Object Explorer to be filled and point to the right location – Stack Overflow

Posted by jpluimers on 2014/03/25

As a SQL Server developer, most of us make extensive use of SQL Server Management Studio.

With lots of production database, I have a directory full of shorcuts that pre-fill server/database/… on the command-line.

One of the drawbacks these shortcuts have is that the Object Explorer stays empty, and does not sync with the currently active Server/Database.

While browsing to solve that, I found that the

Alt+F8 shortcut will focus the SERVER in the Object Explorer.

This was explained by StackOverflow user Andrei Rantsevich who had this great answer on StackOverflow: Read the rest of this entry »

Posted in Database Development, Development, SQL Server, SQL Server 2008, SQL Server 2008 R2, SQL Server 2012 | Leave a Comment »

%d bloggers like this: