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

查看:246
本文介绍了使用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天全站免登陆