Casting an Interface Reference to the Implementing Class in Delphi 2010

Image ©2007-2009 ~FractalAngel-Stock Not all the new features in Delphi 2010 are big. In fact, the team have spent a lot of time implementing many small features, fixes and tweaks. Some of these may not seem to amount to much individually, but they not only add up to significant impact, but greatly add to the polish of the product. I expect this will be one of those releases that keeps serving up little delights and surprises for a long time.

One of the features in Delphi 2010 that I expect will spawn much debate is the ability to cast an interface reference back to the type of class that implements it.

Let’s say we have an Interface, IMyInterface, and a class that implements that Interface, TMyClass:

compileerror Further, let’s say we’ve been passed a variable of type IMyInterface. What happens if we want to invoke Bar? Attempting to simply cast the interface reference to a TMyClass would result in a compiler error.

The most common solution to this I’ve seen is to have a method in the interface that returns the class type, but this kind of defeats much of the value of the interface in the first place, tying the interface to a specific implementation. Further, it’s ugly as a hatful.

Well, such hacks are no longer required.

In Delphi 2010, you can now use the is operator to test to see if an interface type is implemented by a specific class, and if so, cast it to that type and reference any non-interface methods, properties, etc.

Further, the as operator will throw an EInvalidCast exception if you attempt to cast an interface reference to a class type that it was not actually extracted from. The hard cast will return nil in the same circumstances. (Thanks Allen for the correction!)

So this code now runs happily:

This needs to be used with some intelligence, of course. For example, the normal warnings about storing a reference-counted interface and a non-reference counted object reference to the same instance still apply.

18 Comments

  • Cool. Small detail, you might want to update the image, you are calling Foo instead of Bar.

  • Actually the “as” cast will still raise an EInvalidCast exception if the interface isn’t implemented by the specific object type. The hard-cast will return nil if the interface is not implemented by the casting object.

    Allen.

  • Awesome, I make use of non refcounted (AddRef etc overridden) interfaced objects to share with C++ and avoid troublesome circular references in complex situations. I generally have a GetObject method in each interface to get to the implementing class, just perfect!

    It would be cool if I could disable the automatic refcounting for these sepecific non-refcounted objects altogether as well.

    Thanks for sharing Malcolm, keep the “small” stuff coming!

  • Twitter Comment


    Casting an Interface Reference to the Implementing Class in Delphi 2010 [link to post]

    Posted using Chat Catcher

  • This is really cool! I’ve seen people implement this through horrible hacks, and so I think you’re right, it’ll be very nice to have this as a supported feature.

    Will this work in C++?

  • […] Malcolm Groves shows off YACLF* in Delphi 2010. […]

  • Now THAT is gonna save a few seconds, but more importantly clean up a bit of code.

    You don’t say if any caution has to be exercised when working with interface references to implementing code that is not a Delphi class in the same process.

    Does this simply yield a NIL or do you have to exercise caution when (potentially) applying this method to such interface references?

  • very good!!! using inteface like this is more readable and intutive. Good work, tnkx

  • J.D. : Ahh, good catch. Thanks.

    Allen : Thanks for that, I’ll update the post.

  • I attended one of Danny’s “Reading Tea Leaves: The Fine Art of Debugging” sessions at BorCon several years back, so I know that this isn’t so trivial. I’d love to hear some gritty details about how this was implemented, and how you make it work even though the interface in question might be COM and not follow Delphi conventions.

  • Hi Jolyon,

    I’ll have to check, but I think it only works if the class that implements the interface is a Delphi class. I’ll check it out though.

    Cheers
    Malcolm

  • […] that in mind, I thought it very much a Good Thing when Malcolm Groves reported how yet another COM-related quirk — namely, the inability to cast from an interface to an […]

  • […] mentioned earlier that a lot of work in this release had gone into smaller features and feature tweaks. One […]

  • Would it be possible to have a way of getting an interface name from a GUID ?

    If the interface above is served up by a factory, done by something like Factory.GetObject(IMyInterface)

    and lets say that for some reason the object that implements this class hasn’t been registered, then when debugging, the developer gets a message something like

    ‘Could not find object for 7AD04980-5869-4C93-AC29-C26134511554′

    … Which isn’t particularly useful.

    It would be nice if the compiler could somehow match the interface name with the GUID.

    I have written a small script that generates a list of GUID, Interface name combinations, but it’s a bit nasty because the file can get out of date if the script is not rerun after a change.

    …Would be nice to have better interface ‘metadata’ support.

    Cheers
    DC

  • Knew I’d seen this mentioned somewhere before. I’m about to use it to invade the internals of my interface instances so I can unit test them.

    Thanks for posting this originally, I think I would have missed it otherwise.

  • […] Groves, no parece casual ya que anteriormente, podemos encontrar en su blog, y concretamente en Casting an Interface Reference to the Implementing Class in Delphi 2010 de fecha 16 deAgosto de 2009, una referencia a una de las novedades introducidas en Delphi 2010, […]

  • […] http://www.malcolmgroves.com/blog/?p=500 says that this is (newly) implemented in Delphi 2010, which strongly suggest that it wasn’t possible before. Is this indeed the case, or is there a way I’m not familiar with? RTTI, maybe? […]

  • […] en su blog varias entradas abordando aspectos relacionados con las interfaces. Concretamente en Casting an Interface Reference to the Implementing Class in Delphi 2010 de fecha 16 deAgosto de 2009, hace una referencia a una de las novedades introducidas en Delphi […]

You must be logged in to post a comment.