简体   繁体   English

为什么IDisposable具有[ComVisible(true)]属性?

[英]Why does IDisposable have the [ComVisible(true)] attribute?

I've inherited a large legacy ASP.NET application where I work, and our company wants us to use FxCop and fix all major rule violations. 我继承了一个大型的旧ASP.NET应用程序,并且在该公司工作,我们公司希望我们使用FxCop并修复所有主要的违反规则的问题。 Currently a rule that seems to be prevalent is the following: 当前,一条似乎普遍的规则如下:

CA1405: COM visible type base types should be COM visible CA1405:COM可见类型的基本类型应为COM可见

It appears that every ASP.NET page/control in our application is violating this rule. 看来我们的应用程序中的每个ASP.NET页面/控件都违反了此规则。 I wasn't sure why at first, but eventually I realized it is due to the following inheritance chain: 起初我不确定为什么,但是最终我意识到这是由于以下继承链造成的:

System.IDisposable
  → System.Web.UI.Control
    → System.Web.UI.TemplateControl
      → System.Web.UI.Page

I also realized IDisposable is defined as: 我也意识到IDisposable被定义为:

[ComVisible(true)]
public interface IDisposable
{
  void Dispose();
}

To summarize: all ASP.NET pages/controls inherit from IDisposable , and IDisposable has the [ComVisible(true)] attribute, which means all pages/controls will fail this rule. 总结一下:所有ASP.NET页面/控件都继承自IDisposable ,并且IDisposable具有[ComVisible(true)]属性,这意味着所有页面/控件都将失败此规则。

This is hurting us because our application has thousands of pages and controls, and thus thousands of violations. 因为我们的应用程序具有成千上万的页面和控件,因此有成千上万的违规行为,这对我们造成了伤害。 Disabling the rule is not an option, as we have a "company standard" set of FxCop rules. 禁用规则不是一种选择,因为我们有一套“公司标准”的FxCop规则。 It seems that FxCop wants us to go through and put this attribute on every single page in our application, which I really am not interested in doing. 看来FxCop希望我们仔细检查一下这个属性,并将其放在应用程序的每个页面中,我对此确实不感兴趣。

My question is, why does IDisposable have the [ComVisible(true)] attribute in the first place? 我的问题是,为什么IDisposable具有[ComVisible(true)]属性?

IDisposable is marked as COMVisible in order to allow disposal of resources when a .Net component is used in a COM application. IDisposable标记为COMVisible,以便在COM应用程序中使用.Net组件时可以处理资源。

We actually used this heavily when transitioning from a VB6 application to a full .Net application: we were able to reuse .Net functionality (database access) in aa legacy VB6 application, but had to use the IDispose interface to ensure things such as database connections were correctly cleaned up when we were done using the object. 从VB6应用程序过渡到完整的.Net应用程序时,我们实际上大量使用了这一功能:我们能够在旧版VB6应用程序中重用.Net功能(数据库访问),但必须使用IDispose接口来确保诸如数据库连接之类的事情当我们使用完对象后,它们被正确清理了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM