你可以这样做,但问题是你为什么要这样做?
如果您希望引用接口的 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)。
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