<small id='KsG0E'></small><noframes id='KsG0E'>

  1. <tfoot id='KsG0E'></tfoot>
    1. <i id='KsG0E'><tr id='KsG0E'><dt id='KsG0E'><q id='KsG0E'><span id='KsG0E'><b id='KsG0E'><form id='KsG0E'><ins id='KsG0E'></ins><ul id='KsG0E'></ul><sub id='KsG0E'></sub></form><legend id='KsG0E'></legend><bdo id='KsG0E'><pre id='KsG0E'><center id='KsG0E'></center></pre></bdo></b><th id='KsG0E'></th></span></q></dt></tr></i><div id='KsG0E'><tfoot id='KsG0E'></tfoot><dl id='KsG0E'><fieldset id='KsG0E'></fieldset></dl></div>

      <legend id='KsG0E'><style id='KsG0E'><dir id='KsG0E'><q id='KsG0E'></q></dir></style></legend>
      • <bdo id='KsG0E'></bdo><ul id='KsG0E'></ul>

      为什么不鼓励将程序集标记为 ComVisible(true)?

      时间:2023-07-24
          <tbody id='5eabu'></tbody>
          <legend id='5eabu'><style id='5eabu'><dir id='5eabu'><q id='5eabu'></q></dir></style></legend>
              <bdo id='5eabu'></bdo><ul id='5eabu'></ul>

            • <i id='5eabu'><tr id='5eabu'><dt id='5eabu'><q id='5eabu'><span id='5eabu'><b id='5eabu'><form id='5eabu'><ins id='5eabu'></ins><ul id='5eabu'></ul><sub id='5eabu'></sub></form><legend id='5eabu'></legend><bdo id='5eabu'><pre id='5eabu'><center id='5eabu'></center></pre></bdo></b><th id='5eabu'></th></span></q></dt></tr></i><div id='5eabu'><tfoot id='5eabu'></tfoot><dl id='5eabu'><fieldset id='5eabu'></fieldset></dl></div>
              <tfoot id='5eabu'></tfoot>

              1. <small id='5eabu'></small><noframes id='5eabu'>

                本文介绍了为什么不鼓励将程序集标记为 ComVisible(true)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我总是用 [assembly: ComVisible(true)] 将我的 .NET 程序集标记为对 COM 可见,我认为我永远不知道何时有人可能需要从 COM 调用它们.我也开始使用 FxCop 并开始从代码分析中看到这个警告:

                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(false) 标记它,然后用 ComVisible(true) 标记程序集中应该向 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 接口不是免费的,因为存在不兼容和必须满足的要求.这必须考虑并维护.(警告 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.)

                因此,我一直使用选择加入"的理念而不是选择退出",即我没有让所有 COM 可见,而是将程序集标记为 COM 不可见.然后,我专注于有选择地公开类型成员(即通过选择加入),并确保公开的 API 对于 COM 是合理的(例如,COM 不支持泛型、方法重载或带参数的构造函数)并且它具有已在考虑 COM 的情况下进行了测试.通过这种方式,以一种严格、经过测试、有界和可维护的方式向 COM 公开 API.

                Therefore 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 typesmembers 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 generics, 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 等.如果您重构代码并更改方法的顺序或插入重载等,那么任何从您以前的 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.

                这篇关于为什么不鼓励将程序集标记为 ComVisible(true)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:从 64 位代码访问 32 位 DLL 下一篇:阅读 Outlook .msg 文件

                相关文章

              2. <legend id='RyGPG'><style id='RyGPG'><dir id='RyGPG'><q id='RyGPG'></q></dir></style></legend>
                <i id='RyGPG'><tr id='RyGPG'><dt id='RyGPG'><q id='RyGPG'><span id='RyGPG'><b id='RyGPG'><form id='RyGPG'><ins id='RyGPG'></ins><ul id='RyGPG'></ul><sub id='RyGPG'></sub></form><legend id='RyGPG'></legend><bdo id='RyGPG'><pre id='RyGPG'><center id='RyGPG'></center></pre></bdo></b><th id='RyGPG'></th></span></q></dt></tr></i><div id='RyGPG'><tfoot id='RyGPG'></tfoot><dl id='RyGPG'><fieldset id='RyGPG'></fieldset></dl></div>

                  1. <tfoot id='RyGPG'></tfoot>
                      <bdo id='RyGPG'></bdo><ul id='RyGPG'></ul>

                    <small id='RyGPG'></small><noframes id='RyGPG'>