问题描述
我有一个 C# 程序集,用作非托管 C++ 应用程序使用的进程内 COM 服务器.该程序集使用永远不会更改的 Web 服务,因此无需更新 Web 服务代理类.这就是为什么代理类只创建一次,Reference.cs
文件被简单地放入存储库并仅从那里编译.
I have a C# assembly that I use as an in-proc COM server consumed by an unmanaged C++ application. The assembly consumes a webservice that will not ever change so there's no need to ever update the webservice proxy classes. That's why the proxy classes are created once and Reference.cs
files are simply put into the repository and only compiled from there.
问题在于默认情况下,webservice 代理类是公共的,因此暴露给 COM.这会膨胀 typelib 并污染注册表.将可见性更改为内部中断程序集,因此这些实体需要保持公开状态,但不需要向 COM 公开.
The problem is that by default the webservice proxy classes are public and are therefore exposed to COM. This inflates the typelib and pollutes registry. Changing visibility to internal break the assembly, so those entities need to remain public, but need not be exposed to COM.
笨方法是处理 Reference.cs
文件中的每个公共接口/类并用
The dumb way is to approach every public interface/class in the Reference.cs
files and mark it with
[System.Runtime.InteropServices.ComVisible(false)]
之后它不再暴露给 COM.
After that it is no longer exposed to COM.
有没有更好的办法?
推荐答案
你可以
- 将程序集本身标记为不是
ComVisible
,然后将要公开给COM
的所有接口/类/枚举显式标记为ComVisible
. 使用第二个文件和部分类将您的代理类型标记为
ComVisible(false)
- mark the assembly itself to not to be
ComVisible
and then explicitly mark all interfaces/classes/enums you want to expose toCOM
asComVisible
. use a 2nd file and partial class to mark your proxy types to be
ComVisible(false)
[System.Runtime.InteropServices.ComVisible(false)]
partial class YourProxyType {}
[System.Runtime.InteropServices.ComVisible(false)]
partial class AnotherProxyType {}
这篇关于如何优雅地防止 Web 服务代理暴露给 COM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!