在不修改接口的情况下,将delphi接口转换为实现类 [英] Casting a delphi interface to its implementation class without modifying the interface

查看:129
本文介绍了在不修改接口的情况下,将delphi接口转换为实现类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何在Delphi中将接口转换为对象

使用Delphi 5;我有一个接口,我不能因为遗留原因而改变。我正在传递(指向),界面遍布整个地方。实现类有几个新属性 - 有没有办法强制从接口到实际执行的转换?

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 说,这是(新)在Delphi 2010中实现的,其中强烈建议以前是不可能的。确实是这样,还是有一种我不熟悉的方式? RTTI,可能吗?

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?

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

(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.)

推荐答案


  • 经典的作品是由Hallvard Vassbotn: Hack#7:Object to Object

  • 最近,Delphi编译工程师Barry Kelly也提供了一个实现:对象转换接口的一个难看的替代方法

    • The classic take on this is by Hallvard Vassbotn: Hack #7: Interface to Object
    • More recently Barry Kelly, a Delphi compiler engineer, also provided an implementation: An ugly alternative to interface to object casting
    • 我认为这两种方法都可以正常工作。

      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.

      这篇关于在不修改接口的情况下,将delphi接口转换为实现类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆