为什么Delphi变种不能保存对象? [英] Why can't Delphi variants hold objects?

查看:153
本文介绍了为什么Delphi变种不能保存对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么Delphi变种不能持有对象?更重要的是,这个限制背后的原因是什么?

解决方案

这是一个意见,从我的经验来看,哪些变体可以和不能做。



如果将COM对象放入其中,它将作为IDispatch引用存储,因此您访问此对象的任何方法调用或属性将被转换为查找内部DISPID的代码将构造方法/属性,一个包含方法参数的数组,并通过IDispatch接口调用该方法。



换句话说,为您处理IDispatch,你通常必须这样做,但是它是由编译器自动完成的。



然而,对于普通的Delphi对象,事情变得更加困难。您可以使用RTTI查找并调用已发布的方法和属性,但这是关于它的。如果您有非发布的非虚拟方法的名称,则Delphi将无法在您的方法中找到正确的地址。



换句话说,所有您将能够做的只是保持对象,您将无法使用该对象。也许他们可以添加支持,只是释放它,但再次,这可能是它。



我知道一个事实,如果你实现IDispatch正确,你可以安全地存储,并通过变体使用该对象。我有一个类可以作为您要执行此操作的Delphi对象的基类。它将自动公开已发布的方法/属性,如果您想通过一些受保护的方法调用,您可以添加更多。如果对这样一个类有兴趣,我可以把它放在某个地方。



但是,这是通过IDispatch,它使用已发布的方法,其余的是手动代码,所以对您的对象的支持必须由您构建。



这是为什么我认为他们只是说:这只会产生投诉,我们可以持有一个对象,但它只是没用。



但这只是我的想法。也许某官员有一个更好的答案。


Why can't Delphi variants hold objects? More importantly, what's the reason behind this limitation?

解决方案

This is just an opinion, from my experience with what variants can and cannot do.

If you put a COM object into it, it will be stored as an IDispatch reference, and thus any method calls or properties you access on this object will be converted into some code that looks up the internal DISPID of the method/property, an array with method arguments will be constructed, and the method will be invoked through the IDispatch interface.

In other words, IDispatch is handled for you, the way you would normally have to do it, but it's done automagically by the compiler.

However, for normal Delphi objects, things get harder. You can use RTTI to find, and call published methods and properties, but that's about it. If you have the name of a non-published, non-virtual method, Delphi can't find the right address for it on your method.

In other words, all you would be able to do would be to just hold the object, you wouldn't be able to use it. Perhaps they could add support for just freeing it, but again, that would probably be it.

I know for a fact that if you implement IDispatch correctly, you can safely store, and use the object through a variant. I have a class that can be used as the base class for Delphi objects you want to do this on. It will automatically expose published methods/properties, and you can add more if you want through some protected method calls. If there is an interest in such a class I can place it somewhere.

But again, this is through IDispatch, and it uses the published methods, the rest is manual code, so the support for variants have to be built into your objects, by you.

Which is why I think they just said: This will just generate complaints, that we can hold an object but it's just useless.

But that's just my thoughts. Perhaps someone official have a much better answer.

这篇关于为什么Delphi变种不能保存对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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