Casting an Interface Reference to the Implementing Class in Delphi 2010
Translations of this material:
- into Russian: Преобразование ссылки на интерфейс для реализации класса в Delphi 2010. Translation complete.
-
Submitted for translation by r3code 01.02.2010
Published 1 year, 11 months ago.
Text
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:
IMyInterface = interface
['{7AD04980-5869-4C93-AC29-C26134511554}']
procedure Foo;
end;
TMyClass = class(TInterfacedObject, IMyInterface)
procedure Foo;
procedure Bar;
end;
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:
if MyInterface is TMyClass then
TMyClass(MyInterface).Bar;
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.
