How to cast a Interface to a Object in Delphi

前端 未结 6 1026
南旧
南旧 2020-12-01 18:39

In delphi 2009 I have a reference to a IInterface which I want to cast to the underlying TObject

Using TObject(IInterface) obv

6条回答
  •  伪装坚强ぢ
    2020-12-01 19:07

    You are right. Beginning with Delphi 2010, you are able to use the as operator, e.g. via aObject := aInterface as TObject or even aObject := TObject(aInterface).

    This as operator use a special hidden interface GUID (ObjCastGUID) to retrieve the object instance, calling an enhanced version of TObject.GetInterface, which does not exist prior to Delphi 2010. See source code of System.pas unit to see how it works.

    I've published some code working for Delphi 6 up to XE2, including Delphi 2009.

    See http://blog.synopse.info/post/2012/06/13/Retrieve-the-object-instance-from-an-interface

提交回复
热议问题