将delphi接口转换为其实现类而不修改接口
可能的重复:
如何在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
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.
向我的老板致意,答案是:使用非常有用的 JEDI 库,特别是
GetImplementorOfInterface
方法。H/t to my boss, the answer is: use the incredibly useful JEDI library, specifically the
GetImplementorOfInterface
method.我做了“可能重复”问题接受的答案所做的事情:
让对象实现
IObject
接口:因此,它会是:
更新:为了深入理解这一点,您可以向现有对象添加新的接口。
现有对象:
添加
IObject
作为其公开的接口之一:典型用法:
i do what the "possible duplicate" question's accepted answer does:
Have the object implement the
IObject
interface:So it would be:
Update: To drive the point home, you can add a new interface to an existing object.
Existing object:
Add
IObject
as one of the interfaces it exposes:Typical usage: