在 Delphi 中使用接口是否需要 GUID? [英] Are GUIDs necessary to use interfaces in Delphi?
问题描述
官方文档说它们是可选的.我知道 COM 互操作需要每个接口的唯一标识符,但我看到的每个接口示例都有一个 GUID,无论它是否与 COM 一起使用?如果不与 COM 一起使用,包含 GUID 是否有任何好处?
The official documentation says they are optional. I know COM interop requires a unique identifier for each interface but every interface example I see has a GUID whether it's used with COM or not? Is there any benefit to including a GUID if its not going to be used with COM?
推荐答案
我注意到一些方法例如 Supports
(确定一个类是否符合特定接口)要求您在使用它们之前定义一个 GUID.
I've noticed that some methods such as Supports
(to determine if a class conforms to a specific interface) require that you define a GUID before you can use them.
本页确认以下信息:
注意:SysUtils 单元提供了一个重载 函数称为 Supports上课时返回真或假类型和实例支持由 a 表示的特定接口GUID. Supports 函数用于德尔福的方式是和作为运营商.显着差异是 Supports 函数可以取作为右操作数 GUID 或与一个关联的接口类型GUID,而 is 和 as 取名字的一种.有关更多信息is 和 as,请参阅类引用.
Note: The SysUtils unit provides an overloaded function called Supports that returns true or false when class types and instances support a particular interface represented by a GUID. The Supports function is used in the manner of the Delphi is and as operators. The significant difference is that the Supports function can take as the right operand either a GUID or an interface type associated with a GUID, whereas is and as take the name of a type. For more information about is and as, see Class References.
这里有一些关于接口的有趣信息,其中指出:
Here's some interesting information about interfaces, which states:
为什么需要一个接口唯一可识别?答案是简单:因为 Delphi 类可以实现多个接口.当一个应用程序正在运行,必须是一种获取指针的机制从一个适当的接口执行.唯一找到的方法如果一个对象实现了一个接口并获取指向该接口的实现是通过 GUID.
Why does an interface need to be uniquely identifiable? The answer is simple: because Delphi classes can implement multiple interfaces. When an application is running, there has to be a mechanism that will get pointer to an appropriate interface from an implementation. The only way to find out if an object implements an interface and to get a pointer to implementation of that interface is through GUIDs.
在两个引号中都添加了强调.
阅读整篇文章还会让您意识到QueryInterface
(需要 GUID)在幕后使用,例如引用计数.
Reading this entire article also makes you realize that QueryInterface
(which requires a GUID) is used behind the scenes for reasons such as reference counting.
这篇关于在 Delphi 中使用接口是否需要 GUID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!