使用 ComVisible(false) 时指定 Guid 有什么意义吗? [英] Is there any point in specifying a Guid when using ComVisible(false)?

查看:12
本文介绍了使用 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 有什么意义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆