为什么标志着装配标记有ComVisible特性(真)劝阻? [英] Why is marking an assembly ComVisible(true) discouraged?

查看:1398
本文介绍了为什么标志着装配标记有ComVisible特性(真)劝阻?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直都打上我的.NET程序集作为COM可见与 [总成:标记有ComVisible特性(真)] ,以为我不知道什么时候有人会需要给他们打电话从COM。我也开始使用的FxCop,并开始看到从code解析这样的警告:

I have always marked my .NET assemblies as visible to COM with [assembly: ComVisible(true)], thinking that I never know when someone might need to call them from COM. I also started using FxCop and started seeing this warning from code analysis:

CA1017:Microsoft.Design:因为MyLibrary.dll公开外部可见的类型,用标记有ComVisible特性(假)在汇编级别标记,然后标记所有类型的组件内,应与标记有ComVisible特性暴露给COM客户端(真)

CA1017 : Microsoft.Design : Because 'MyLibrary.dll' exposes externally visible types, mark it with ComVisible(false) at the assembly level and then mark all types within the assembly that should be exposed to COM clients with ComVisible(true)

是有一些原因,为什么你会不会只是想的所有的你的公共类型暴露给COM?我猜是有,但我无法想象这是什么原因。如果有什么事情,似乎明显不便。

Is there some reason why you would not simply want all of your public types exposed to COM? I'm guessing that there is, but I cannot imagine what this reason is. If anything, it seems markedly inconvenient.

推荐答案

我还没有与COM互操作相当一段时间,但在过去我一直曾与选入的理念,而不是OPT-出,即而不是让一切COM可见,我纪念集会不COM可见。然后,我专注于揭露类型\成员选择性(即通过选择的),并确保已公开的API是明智的COM(如COM不支持Genrics,方法重载或构造内搭参数),而且它有经过测试与COM的初衷。以这种方式暴露一个API,COM​​在严格完成,经过测试,有界和维护的方式。

I have not worked with COM interop for quite some time, but in the past I have always worked with the "opt-in" philosophy rather than "opt-out" i.e. rather than making everything COM visible, I mark the assembly as not COM visible. I then concentrate on exposing types\members selectively (i.e. by opting in), and make sure that the API that is exposed is sane for COM (e.g. COM does not support Genrics, method overloading or constructors which take parameters) and also that it has been tested with COM in mind. In this way exposing an API to COM is done in a rigorous, tested, bounded and maintainable fashion.

这是相反的,以使一切COM可见再担心任何潜在的问题之后,铭记,如果已经暴露一切那么有可能是您的COM接口的用户联轴器,你没想到,现在将有难打了退堂鼓了。

This is the opposite to making everything COM visible and then worrying about any potential issues later, bearing in mind that if you have exposed everything then there may be couplings with users of your COM interface that you did not expect and will now have difficulty backing out of.

从内存中一对夫妇的意外后果的例子:

From memory a couple of examples of unexpected consequences:

  1. 在出口重载的方法,他们出口,并命名为默认情况下使用的序列号,例如OverloadedMethod1,OverloadedMethod2等,如果你重构你的code和改变你的方法的顺序或插入过载,等等,你再与任何人从你的previous COM接口使用这些方法的麻烦。 OverloadedMethod1和OverloadedMethod2可能已经交换了。

  1. When exporting overloaded methods, they are exported and named by default with a sequence number e.g. OverloadedMethod1, OverloadedMethod2, etc. If you refactor your code and change the order of your methods or insert an overload, etc, you are then in trouble with anyone that has used these methods from your previous COM interface. OverloadedMethod1 and OverloadedMethod2 may have been swapped.

班它们暴露于COM必须有一个参数的构造函数。如果有不到位的一个单元测试,维护本合同,那么很容易改变这一类在以后的日子,这样它没有参数的构造函数,因此伤了你的COM接口的用户。

Classes which are exposed to COM must have a parameterless constructor. If there is not a unit test in place that maintains this contract, then it is easy to change that class at a later date so that it does not have a parameterless constructor and thus breaks your COM interface users.

最关键的一点是,导出一个COM接口并没有免费的午餐,因为有不兼容,并要求必须得到满足。这必须既想过,然后保持。警告CA1017是暗指这一点。

The key thing is that exporting a COM interface does not come for free as there are incompatibilities and requirements that must be met. This has to be both thought about and then maintained. Warning CA1017 is alluding to this.

这篇关于为什么标志着装配标记有ComVisible特性(真)劝阻?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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