Delphi XE introduced SafeIntfAsClass to cast back interface to class (via: oop – Delphi – Proxy Design Pattern – interface problem – Stack Overflow)
Posted by jpluimers on 2011/03/23
Delphi XE introduced the SafeIntAsClass to cast back from an interface to a class.
This is one of the things I really started to miss after doing a lot of .NET work (where that worked starting with .NET 1).
Older versions of Delphi did not support safely casting from an interface back to an object.
–jeroen
via: oop – Delphi – Proxy Design Pattern – interface problem – Stack Overflow.






Stefan said
If you need to cast an interface reference back to an object reference, you’re doing something wrong design wise imo.
jpluimers said
You don’t need this often, but sometimes you want to get on the object side of your platform in stead of the interface side.
This holds for any platform, Delphi, .NET, etc.
–jeroen
CR said
It was D2010 actually (Allen Bauer blogged about it at the time). Moreover, your highlighting something called ‘SafeIntAsClass’ might be a bit confusing out of context, since the syntax is to use either an apparently hard cast or the ‘as’ operator [i.e., MyObj := TMyObj(MyIntf) or MyObj := MyIntf as TMyIntf]. If the class type queried for is incorrect, the ‘hard’ cast returns nil (like what Delphi .NET used to do) where the ‘as’ operator raises an exception.
I agree it’s a neat little addition though, even if the old problems of the IInterface=IUnknown equation aren’t magically got rid of…
jpluimers said
I think Allen only talked about the as cast, but I’ve put the references at http://stackoverflow.com/questions/4861280/what-are-the-pros-and-cons-of-using-interfaces-in-delphi/4982273#comment59217492_4982273