Is there any point in specifying a Guid when using ComVisible(false)?(使用 ComVisible(false) 时指定 Guid 有什么意义吗?)
问题描述
在 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 有什么意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 ComVisible(false) 时指定 Guid 有什么意义吗?
基础教程推荐
- SSE 浮点算术是否可重现? 2022-01-01
- 为什么Flurl.Http DownloadFileAsync/Http客户端GetAsync需要 2022-09-30
- 有没有办法忽略 2GB 文件上传的 maxRequestLength 限制? 2022-01-01
- rabbitmq 的 REST API 2022-01-01
- c# Math.Sqrt 实现 2022-01-01
- 如何在 IDE 中获取 Xamarin Studio C# 输出? 2022-01-01
- 将 XML 转换为通用列表 2022-01-01
- 如何激活MC67中的红灯 2022-01-01
- MS Visual Studio .NET 的替代品 2022-01-01
- 将 Office 安装到 Windows 容器 (servercore:ltsc2019) 失败,错误代码为 17002 2022-01-01