将delphi接口转换为其实现类而不修改接口

发布于 2024-11-16 07:49:41 字数 710 浏览 7 评论 0原文

可能的重复:
如何在 Delphi 中将接口转换为对象

使用Delphi 5;我有一个由于遗留原因无法更改的界面。我正在到处传递(指向)该接口。实现类有几个新属性 - 有没有办法强制从接口到实际实现的转换?

http://www.malcolmgroves.com/blog/?p=500 说这是在 Delphi 2010 中(新)实现的,这强烈表明这在以前是不可能的。确实如此,还是有我不熟悉的方法? RTTI,也许?

(我检查过,如果 pScore 是 TOleScore 那么 Delphi 5 编译器确实不允许 - 这里 pScore 是我的 pScore: IScore 参数, TOleScore 是实现类。)

Possible Duplicate:
How to cast a Interface to a Object in Delphi

Using Delphi 5; I have an interface that I cannot change for legacy reasons. I am passing (pointers to) that interface all over the place. The implementing class has several new properties - is there a way to force a cast from the interface to the actual implementation?

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?

(I checked, and if pScore is TOleScore then is indeed not allowed by the Delphi 5 compiler - here pScore is my pScore: IScore argument, and TOleScore is the implementing class.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

客…行舟 2024-11-23 07:49:41

I认为这两种方法都应该有效。


顺便问一下,有人知道 Hallvard 是否还活跃吗?过去几年我没有见过他。

I think both approaches should work.


Incidentally, does anyone know if Hallvard is still active? I've not come across him in the past few years.

梦幻的味道 2024-11-23 07:49:41

向我的老板致意,答案是:使用非常有用的 JEDI 库,特别是 GetImplementorOfInterface 方法

H/t to my boss, the answer is: use the incredibly useful JEDI library, specifically the GetImplementorOfInterface method.

情独悲 2024-11-23 07:49:41

我做了“可能重复”问题接受的答案所做的事情:

让对象实现IObject接口:

IObject = interface(IUnknown)
    ['{39B4F98D-5CAC-42C5-AF8D-0237C8EFBE4C}']
    function GetSelf: TObject;
end;

因此,它会是:

var
   thingy: IThingy;
   o: TOriginalThingy;

begin
   o := (thingy as IObject).GetSelf as TOriginalThingy;

更新:为了深入理解这一点,您可以向现有对象添加新的接口

现有对象:

type
    TOriginalThingy = class(TInterfacedObject, IThingy)
    public
       //IThingy
       procedure DrinkCokeZero; safecall;
       procedure ExcreteCokeZero; cafecall;
    end;

添加 IObject 作为其公开的接口之一:

type
    TOriginalThingy = class(TInterfacedObject, IThingy, IObject)
    public
       //IThingy
       procedure DrinkCokeZero; safecall;
       procedure ExcreteCokeZero; cafecall;

       //IObject - provides a sneaky way to get the object implementing the interface
       function GetSelf: TObject;
    end;

    function TOriginalThingy.GetSelf: TObject;
    begin
       Result := Self;
    end;

典型用法

    procedure DiddleMyThingy(Thingy: IThingy);
    var
       o: TThingy;
    begin
       o := (Thingy as IObject).GetSelf as TThingy;

       o.Diddle;
    end;

i do what the "possible duplicate" question's accepted answer does:

Have the object implement the IObject interface:

IObject = interface(IUnknown)
    ['{39B4F98D-5CAC-42C5-AF8D-0237C8EFBE4C}']
    function GetSelf: TObject;
end;

So it would be:

var
   thingy: IThingy;
   o: TOriginalThingy;

begin
   o := (thingy as IObject).GetSelf as TOriginalThingy;

Update: To drive the point home, you can add a new interface to an existing object.

Existing object:

type
    TOriginalThingy = class(TInterfacedObject, IThingy)
    public
       //IThingy
       procedure DrinkCokeZero; safecall;
       procedure ExcreteCokeZero; cafecall;
    end;

Add IObject as one of the interfaces it exposes:

type
    TOriginalThingy = class(TInterfacedObject, IThingy, IObject)
    public
       //IThingy
       procedure DrinkCokeZero; safecall;
       procedure ExcreteCokeZero; cafecall;

       //IObject - provides a sneaky way to get the object implementing the interface
       function GetSelf: TObject;
    end;

    function TOriginalThingy.GetSelf: TObject;
    begin
       Result := Self;
    end;

Typical usage:

    procedure DiddleMyThingy(Thingy: IThingy);
    var
       o: TThingy;
    begin
       o := (Thingy as IObject).GetSelf as TThingy;

       o.Diddle;
    end;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文