Hi,

Is there a way to cast an interface to an object?

If I have an interface:

IBlub = interface
  function GetSomething(): TObject;
end;

And somewhere, I have:

var
  Blub: IBlub;
...

Therefore, Blub is an interface-reference to some object which implements the interface. How can I get this TObject? A simply cast like TObject(Blub) surely does not work, because Blub points to the function table of the interface. But Blub itself has to save the object somewhere because if I now call Blub.GetSomething(), I have Self inside of GetSomething.

I could make the following implementation for GetSomething:

function TSomeBlub.GetSomething(): TObject;
begin
  Result := Self;
end;

(I would rename it to GetSelf or GetImplementingObject or perhaps GetImplementingClass (then I would return Self.ClassType).)

But is there a better, direct way? It seems like a hack/workaround to implement such a function.

Albert

Reply via email to