有关将GUID分配给接口的问题(Question about assigning GUID to interface)

系统教程 行业动态 更新时间:2024-06-14 17:00:14
有关将GUID分配给接口的问题(Question about assigning GUID to interface)

我在Delphi XE中编写了这段代码,它从GUID const中分配接口的GUID。 代码编译好了,但我想知道这是否是一个有效的声明?

const IID_IFoo: TGUID = '{00000000-6666-6666-6666-666666666666}'; type IFoo = interface(IDispatch) [IID_IFoo] //properties //methods end;

I wrote this code in Delphi XE which assign the GUID of the interface from GUID const. the code compile ok, but I want to know if this a valid declaration?

const IID_IFoo: TGUID = '{00000000-6666-6666-6666-666666666666}'; type IFoo = interface(IDispatch) [IID_IFoo] //properties //methods end;

最满意答案

可以这样做,但问题是你为什么要这样做?

如果您希望引用接口的GUID而不是接口的名称,那么只要该接口具有关联的IID(GUID),您就可以使用期望TGUID的接口名称:

type IFoo = interface(IDispatch) ['{00000000-6666-6666-6666-666666666666}'] //properties //methods end; // meanwhile, elsewhere in the project... sFooIID := GUIDToString(IFoo);

这是一个较少“嘈杂”的接口声明,并避免了你可能声明/引用一个IID常量的可能性,这个IID常量实际上与你认为的接口无关(或者根本没有与那个IID相关联)。 )。

const IID_Foo = '{00000000-6666-6666-6666-666666666666}'; IID_Bar = '{00000000-6666-6666-6666-777777777777}'; type IFoo = interface(IDispatch) [IID_Bar] // WHOOPS! : end; IBar = interface(IDispatch) // WHOOPS again!! : end; // Meanwhile, elsewhere in the project sBarID := GUIDToString(IID_Bar); // Works, but is the IID of IFoo, not IBar sFooID := GUIDToString(IID_Foo); // Works, but is an IID not associated with any interface

使用接口本身作为接口 IID,而不是单独的const声明,消除了这些错误的可能性。

当为IID使用单独的常量声明时 - 如果你绝对必须 - 你可以通过使用IID所需的接口来防止这些问题之一。 但是,如果将错误的IID用于特定接口,这可能会使事情变得更糟:

// Cannot make the mistake of using an interface as a GUID if it has no IID at all sBarID := GUIDToString(IBar); // Does not compile - IBar has no IID // But if it's the wrong IID then you get results that are "correct" but not expected: a := GUIDToString(IFoo); b := GUIDToString(IID_Foo); a <> b

You can do this, but the question is why would you want to ?

If you wish to refer to the GUID of an interface rather than the name of the interface, then as long as that interface has an associated IID (GUID) then you can use the interface name where a TGUID is expected:

type IFoo = interface(IDispatch) ['{00000000-6666-6666-6666-666666666666}'] //properties //methods end; // meanwhile, elsewhere in the project... sFooIID := GUIDToString(IFoo);

This is a less "noisy" declaration of the interface and avoids the possibility that you might declare/reference an IID constant which isn't actually associated with the interface you think it is (or which hasn't been associated with that IID at all).

const IID_Foo = '{00000000-6666-6666-6666-666666666666}'; IID_Bar = '{00000000-6666-6666-6666-777777777777}'; type IFoo = interface(IDispatch) [IID_Bar] // WHOOPS! : end; IBar = interface(IDispatch) // WHOOPS again!! : end; // Meanwhile, elsewhere in the project sBarID := GUIDToString(IID_Bar); // Works, but is the IID of IFoo, not IBar sFooID := GUIDToString(IID_Foo); // Works, but is an IID not associated with any interface

Using the interface itself as both interface and IID, rather than having a separate const declaration, removes the potential for these mistakes.

When using separate constant declarations for IID's - if you absolutely have to - you can protected against one of these problems by never-the-less using interfaces where IID's are expected. But this arguably makes things worse in the case where the wrong IID has been used for a particular interface:

// Cannot make the mistake of using an interface as a GUID if it has no IID at all sBarID := GUIDToString(IBar); // Does not compile - IBar has no IID // But if it's the wrong IID then you get results that are "correct" but not expected: a := GUIDToString(IFoo); b := GUIDToString(IID_Foo); a <> b

更多推荐

本文发布于:2023-04-18 00:50:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/eccc862305bd8301c19b33fd1a40bdb8.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:接口   GUID   Question   interface   assigning

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!