为什么首先需要 GUID 属性?

发布于 2024-07-25 16:53:09 字数 42 浏览 3 评论 0原文

GUID 属性的必要性是什么? 为什么不让编译器自动处理这个问题?!

What is the necessity for the GUID attribute? why don't just let the compiler handle this automatically?!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

·深蓝 2024-08-01 16:53:09

如果编译器自动处理这个问题,您最终会遇到两种情况之一。

  • 每次编译时都会有一个新的 GUID - 因为 GUID 应该被发布,所以这会失败。

  • 冲突 - 如果 GUID 每次都相同,基于(例如)名称的哈希值,多个项目最终将使用相同的 GUID 来实现不同的目的。

现有的方法 - 显式 GUID 使开发人员能够根据需要控制这些。


实际上,您只需要在有限的情况下添加 GUID 属性 - 主要是用于 COM 互操作(如果您使用 .NET 编写 COM 对象以供另一个系统使用)。 根据您的项目类型,这可能是一项日常活动,也可能是一年一度的活动。

If the compiler handled this automatically, you'd end up with one of two situations.

  • A new GUID every time you compiled - since GUIDs are supposed to be published, this would fail.

  • Collisions - if the GUID was the same every time, based on (say) a Hash of the name, multiple projects would end up using the same GUID for different purposes.

The existing approach - an explicit GUID gives developers the power to control these as required.


You only actually need to add a GUID attribute in limited cases - the primary one being for COM interop, if you're using .NET to write a COM object for use by another system. Depending on what kind of project you have, this might be an everyday event - or an annual one.

染柒℉ 2024-08-01 16:53:09

这些属性对于 COM 来说非常重要。 它是 .NET 的前身,在 20 世纪 90 年代达到全盛​​时期,之后 Java 才抢尽风头。 .NET 需要与 COM 兼容才能有成功的机会。 或者换句话说,您需要能够用 .NET 语言编写大型遗留程序可以使用的 COM 服务器。

[ComVisible] 属性确保 COM 客户端程序可以看到并使用 IEnumerable 接口。 对于允许客户端程序枚举 .NET 集合至关重要。

[Guid]属性在COM中至关重要,它标识了一个接口。 这是通过 guid 而不是名称来完成的,以确保它在不同程序员编写的多个应用程序中是唯一的。 .NET 也有这个功能,但是使用了一个名称来方便人类使用。 “System.Collections.IEnumerable,mscorlib,版本=2.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089”。

IEnumerable<> 是通用版本,没有 [Guid]。 泛型与 COM 不兼容。 如今这已经不重要了,不再有太多可见的 COM,其中大部分已被友好的 .NET 类包装。 但在 Windows 中仍然非常核心,特别是在全新的 WinRT(又名 Metro、又名 Modern UI、又名 UWP)中。 您也不直接使用它,这使得 COM 有点像 Windows 编程的汇编语言。

These are attributes that matter a great deal to COM. Which was the predecessor of .NET and had its heyday in the nineties, before Java stole the show. .NET needed to be compatible with COM to have a chance of succeeding. Or in other words, you needed to be able to write a COM server in a .NET language that a large legacy program could use.

The [ComVisible] attribute ensures that a COM client program can see and use the IEnumerable interface. Essential to allow the client program to enumerate .NET collections.

The [Guid] attribute is crucial in COM, it identifies an interface. Which is done by a guid, not a name, to ensure that it is unique across multiple applications written by different programmers. .NET has this too, but however uses a name to make it easier on humans. "System.Collections.IEnumerable, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089".

IEnumerable<>, the generic version, doesn't have a [Guid]. Generics are not compatible with COM. It doesn't much matter these days, not much visible COM around anymore, most of it has been wrapped by friendly .NET classes. But still very core in Windows, notably in the brand-new WinRT (aka Metro, aka Modern UI, aka UWP). You don't use that directly either, making COM somewhat like the assembly language of Windows programming.

皇甫轩 2024-08-01 16:53:09

您可以这样做(只需省略该属性),但即使接口没有更改,编译器也会在每次重新编译时生成新的 GUID。 不幸的是,因为该界面的用户不知道更改,并且将通过旧的 GUID 检索该界面,因此无法检索它。

You can do it (just omit the attribute) but then the compiler will generate a new GUID on each recompile even if the interface has not changed. That's unfortunate because the users of that interface don't know about the change and will retrieve the interface by it's old GUID and will therefore fail to retrieve it.

老街孤人 2024-08-01 16:53:09

有时您想为某些类或模块提供一个唯一的标识符,该标识符是恒定的且硬编码在源代码中。

Sometimes you want to give certain classes or modules a unique identifier that is constant and hard coded inside your source.

呆萌少年 2024-08-01 16:53:09

要阅读此定义,您需要查找每个属性的含义。 第一个 ComVisibleAttribute,描述如下:

控制单个托管类型或成员或程序集中所有类型对 COM 的可访问性。

这告诉我们,ComVisible 与 COM 有关,并让我们指定特定类型是否对 COM 程序可见。 页面下方有一个链接,指向有关该属性的用途以及类型库导出器如何使用该属性的更多详细信息。

第二个,GuidAttribute,一开始有点不太有用:

当不需要自动 GUID 时提供显式 System.Guid

,但同样,您必须向下阅读其余部分,您将看到另一个关于类型库导出器的提及。

将这两个放在一起,我们就可以清楚地看出,这两个属性控制着导出到类型库时如何处理 IEnumerator。 如果您不知道类型库是什么,这对您来说可能没有多大意义。 如果您不使用 COM 互操作,则可以安全地忽略这些属性。 如果您使用COM互操作,则需要知道Guid才能从非托管COM代码正确访问接口。

微软将它们放在每个接口定义中,以备您需要时使用; 阅读 MSDN 页面的部分技巧是识别此类信息并知道它何时对您没有任何用处。 既然您知道这两个属性的用途,您应该能够确定它们是否与您相关,否则忽略它们。

To read this definition you would need to look up the meaning of each of those attributes. The first, ComVisibleAttribute, is described as this:

Controls accessibility of an individual managed type or member, or of all types within an assembly, to COM.

That tells us that ComVisible is something to do with COM, and lets us specify whether a particular type is visible to COM programs. Further down on the page is a link to more details on what the attribute is for and how its used by the type library exporter.

The second, GuidAttribute, is a bit less helpful at first:

Supplies an explicit System.Guid when an automatic GUID is undesirable

but again, you have to read the rest of the way down, and you will see another mention of the type library exporter.

Putting these two together, it starts to become clear that these two attributes control how IEnumerator is processed when exported to a type library. If you don't know what a type library is, this will probably not mean much to you. If you are not using COM interop, then those attributes can safely be ignored. If you are using COM interop, you would need to know the Guid to properly access the interface from unmanaged COM code.

Microsoft puts these on every interface definition in case you need them; part of the skill in reading the MSDN pages is to recognize this type of information and know when it isn't any use to you. Now that you know what those two attributes are for, you should be able to figure out if they are relevant to you, and ignore them otherwise.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文