在 Visual Studio 中创建新的 C# 项目时,生成的 AssemblyInfo.cs 文件包含一个指定程序集 GUID 的属性.属性上方的注释声明它用于如果此项目暴露给 COM".
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".
我的程序集都不包含需要对 COM 可见的类型,所以我用 [assembly: ComVisible(false)]
标记了我的程序集.那么指定 GUID 有什么意义吗?
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?
我的感觉是答案是否定的——那么为什么默认的 AssemblyInfo.cs 文件同时包含 [assembly: ComVisible(false)]
和 [assembly: 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("...")]
?
总结回答:
在它们之间,答案解释了当且仅当使用 COM 互操作时才需要指定 GUID.因此,在我的情况下,不需要 GUID.
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 进一步解释说 [assembly: ComVisible(false)]
并不意味着不使用 COM 互操作,因为可以为个别类型覆盖 ComVisible
.正是由于这个原因,默认 AssembyInfo.cs 包含 [assembly: ComVisible(false)]
和 GUID.
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.
有 [assembly: ComVisible(false)]
和 [assembly: Guid("...")]
同时非常有意义 在某些情况下.您从一个空程序集开始,并且可能希望将其中的某些内容暴露给 COM.因此,您将程序集标记为非 ComVisible
,然后将要公开的实体标记为 ComVisible
.这就是 GUID 存在的原因默认情况下.
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 not ComVisible
and later mark the entities to expose as ComVisible
. That is why the GUID exists by default.
无论如何,如果您真的不想将程序集中的任何内容暴露给 COM,请不要在项目设置中选中注册 COM 互操作"选项.
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.
这篇关于使用 ComVisible(false) 时指定 Guid 有什么意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!