CA2000传递对象引用到基构造函数在C# [英] CA2000 passing object reference to base constructor in C#

查看:217
本文介绍了CA2000传递对象引用到基构造函数在C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个警告,当我通过运行Visual Studio的代码分析工具一些代码,我不知道如何解决。也许有人在这里碰到过类似的问题,解决它,并愿意分享他们的见解。

I receive a warning when I run some code through Visual Studio's Code Analysis utility which I'm not sure how to resolve. Perhaps someone here has come across a similar issue, resolved it, and is willing to share their insight.

我在编程DataGridView控件使用了自定义绘制的细胞。代码如下所示:

I'm programming a custom-painted cell used in a DataGridView control. The code resembles:

public class DataGridViewMyCustomColumn : DataGridViewColumn
{
    public DataGridViewMyCustomColumn() : base(new DataGridViewMyCustomCell())
    {
    }

它生成以下警告:

CA2000:Microsoft.Reliability:在方法'DataGridViewMyCustomColumn.DataGridViewMyCustomColumn()调用System.IDisposable.Dispose对对象的所有引用前新DataGridViewMyCustomCell()它超出范围。

我的理解是警告我DataGridViewMyCustomCell(或它从继承的类)实现IDisposable接口的Dispose()方法应被调用以清理由DataGridViewMyCustomCell权利任何资源时,它不再。

I understand it is warning me DataGridViewMyCustomCell (or a class that it inherits from) implements the IDisposable interface and the Dispose() method should be called to clean up any resources claimed by DataGridViewMyCustomCell when it is no longer.

我已经看到在互联网上的例子推荐使用块范围对象的生命周期,并在系统自动处理,但基础并不认可时搬进构造体,所以我不能写它周围的使用块...这我不知道我想要做的工作,因为那不是指示运行时释放这可能对象仍然是基类里面以后使用?

The examples I've seen on the internet suggest a using block to scope the lifetime of the object and have the system automatically dispose it, but base isn't recognized when moved into the body of the constructor so I can't write a using block around it... which I'm not sure I'd want to do anyway, since wouldn't that instruct the run time to free the object which could still be used later inside the base class?

我的问题的话,就是代码没关系,是什么?或者说,怎么可能进行重构,以解决警告?我不希望取消此警告,除非它是真正适合这样做。

My question then, is the code okay as is? Or, how could it be refactored to resolve the warning? I don't want to suppress the warning unless it is truly appropriate to do so.

推荐答案

如果你正在使用Visual Studio 2010则是CA2000彻底打破。它也可以在的FxCop(又名代码分析)的其他版本打破,但VS2010是唯一一个我可以保证。我们的代码库是给CA2000警告这样的代码...

If you're using Visual Studio 2010 then CA2000 is completely broken. It may also be broken in other versions of FxCop (a.k.a. Code Analysis), but VS2010 is the only one I can vouch for. Our codebase is giving CA2000 warnings for code like this...

internal static class ConnectionManager 
{
    public static SqlConnection CreateConnection()
    {
         return new SqlConnection("our connection string");
    }
}



...指示该连接不被设置它超出范围的方法之前。嗯,是的,这是真的,但它不是超出范围的应用程序的,因为它是返回给调用者 - 这是该方法的整点!以同样的方式,你的构造参数没有走出去的范围,但被传递给基类,所以它是从规则,而不是一个实际的问题是假阳性。

...indicating that the connection is not being disposed before it goes out of scope in the method. Well, yeah, that's true, but it isn't out of scope for the application as it's returned to a caller - that's the whole point of the method! In the same way, your constructor argument isn't going out of scope but is being passed to the base class, so it's a false positive from the rule rather than an actual problem.

这曾经是一个有用的规则,但现在你真的可以做的是将其关闭,直到他们解决它。这是不幸的,因为(很少)实际阳性的事情,应该是固定的。

This used to be a useful rule, but now all you can really do is turn it off until they fix it. Which is unfortunate, because the (very few) actual positives are things that should be fixed.

这篇关于CA2000传递对象引用到基构造函数在C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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