使用 ComVisible(false) 时指定 Guid 有什么意义吗?
在 Visual Studio 中创建新的 C# 项目时,生成的 AssemblyInfo.cs 文件包含指定程序集 GUID 的属性。该属性上方的注释指出,“如果该项目暴露于 COM”,则使用该属性。
我的程序集都不包含需要对 COM 可见的类型,因此我用 [ assembly: ComVisible(false)]
标记了我的程序集。那么指定 GUID 有什么意义吗?
我的感觉是答案是“否” - 那么为什么默认的 AssemblyInfo.cs 文件同时包含 [ assembly: ComVisible(false)] 和
[ assembly: Guid("... “)]?
编辑:
总结响应:
在它们之间,答案解释了当且仅当使用 COM 互操作时才需要指定 GUID。因此,在我的情况下,GUID 不是必需的。
Sharptooth 进一步解释说,[assemble: ComVisible(false)]
并不意味着不使用 COM 互操作,因为可以覆盖单个类型的 ComVisible
。正是由于这个原因,默认的 AssembyInfo.cs 包含 [assemble: ComVisible(false)]
和 GUID。
When you create a new C# project in Visual Studio, the generated AssemblyInfo.cs file includes an attribute specifying an assembly GUID. The comment above the attribute states that it is used "if this project is exposed to COM".
None of my assemblies contain types which need to be visible to COM, so I have marked my assembly with [assembly: ComVisible(false)]
. So is there any point in specifying a GUID?
My feeling is that the answer is "no" - so why does the default AssemblyInfo.cs file contain both [assembly: ComVisible(false)]
and [assembly: Guid("...")]
?
Edit:
To summarize the responses:
Between them, the answers explain that specifying a GUID is required if and only if COM interop is being used. So, in my situation, a GUID is not necessary.
sharptooth further explains that [assembly: ComVisible(false)]
does not imply not using COM interop, since it is possible to override ComVisible
for individual types. It is for this reason that the default AssembyInfo.cs contains both [assembly: ComVisible(false)]
and a GUID.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
同时拥有
[assemble: ComVisible(false)]
和[assemble: Guid("...")]
非常有意义在某些情况下。您从一个空程序集开始,并且可能希望将其中的某些内容公开给 COM。因此,您将程序集标记为非ComVisible
,然后将要公开的实体标记为ComVisible
。这就是 GUID 默认存在的原因。无论如何,如果您确实不想将程序集中的任何内容公开给 COM,请在项目设置中取消选中“注册 COM 互操作”选项。
Having
[assembly: ComVisible(false)]
and[assembly: Guid("...")]
at the same time makes perfect sense in certain cases. You start with an empty assembly and will perhaps want to expose something from it to COM. So you mark the assembly as notComVisible
and later mark the entities to expose asComVisible
. That is why the GUID exists by default.Regardless, if you really don't want to expose anything from your assembly to COM leave the "Register for COM interop" option unchecked in the project settings.
一致的 GUID 在 COM 中绝对重要。 [ assembly:Guid] 属性生成类型库 LIBID。当然,项目模板会自动生成一个模板,以确保程序员在将 ComVisible 设置为 true 时不会忘记提供一个模板。
如果未提供程序集 [Guid],则 Tlbexp.exe 会根据程序集名称、版本和公钥合成一个。这还不够好,类型库已经有一个版本了。更改 [AssemblyVersion] 将生成不同的 LIBID。尤其糟糕的是,当您使用版本的自动增量选项(例如 1.0.*)时,您可能会很快用大量无效的 TypeLib 注册表项填充注册表。
长话短说,它可以避免很多不幸的事故。
Consistent GUIDs are absolutely essential in COM. The [assembly:Guid] attribute generates the type library LIBID. Surely the project template auto-generates one to make sure that the programmer doesn't forget to provide one when s/he flips ComVisible to true.
If an assembly [Guid] isn't provided then Tlbexp.exe synthesizes one from the assembly name, version and public key. That's not really good enough, type libraries already have a version. Changing [AssemblyVersion] would generate a different LIBID. Especially bad when you use the auto-increment option for the version (like 1.0.*), you could quickly fill the registry with a mountain of dead TypeLib registry keys.
Long story short, it avoids a lot of nasty mishaps.
不,没有真正的理由包含它。除了非常特定的 COM 互操作场景之外,这实际上是没有必要的。尽管我认为拥有一个可以通过反射访问的 GUID 可能会有些用处。但由于它不能保证存在,因此您不能依赖它。
Nope, no real reason to include it. It's really pretty unnecessary except in very specific COM interop scenarios. Though I suppose there could be something useful about having a GUID that you can access with reflection. But since it's not guaranteed to be there, its not like you could rely on it.