简体   繁体   English

是CA1001:TypesThatOwnDisposableFieldsShouldBeDisposable有效吗?

[英]Is CA1001: TypesThatOwnDisposableFieldsShouldBeDisposable Valid?

If I have the following code: 如果我有以下代码:

public class Foo
{
    public void Bar()
    {
        var someTypeWithAnEvent = new SomeTypeWithAnEvent();        

        using (var signal = new ManualResetEvent(false))
        {
            someTypeWithAnEvent.Begun += (sender, e) => signal.Set();
            someTypeWithAnEvent.Begin();
            signal.WaitOne();
        }
    }
}

FxCop seems to throw a CA1001 error: FxCop似乎抛出CA1001错误:

CA1001 : Microsoft.Design : Implement IDisposable on 'Foo' because it creates members of the following IDisposable types: 'ManualResetEvent'. CA1001:Microsoft.Design:在'Foo'上实现IDisposable,因为它创建了以下IDisposable类型的成员:'ManualResetEvent'。

This doesn't seem valid in this instance because I'm disposing of the ManualResetEvent through the using block. 这在这个实例中似乎没有用,因为我通过using块处理了ManualResetEvent

Am I missing something here or is there an error in the rule? 我在这里遗漏了什么或者规则中有错误吗?

Seems like a false warning indeed. 看起来确实像是一个错误的警告。 What version of FxCop are you using? 您使用的是什么版本的FxCop? It is reportedly a bug but might be solved now. 据报道它是一个错误,但现在可能会解决。

Let me guess: you're accessing signal in a lambda expression and the '..' in the error message is a compiler generated class. 让我猜一下:你正在访问lambda表达式中的信号,错误消息中的“..”是编译器生成的类。 In this case it's safe to suppress the message. 在这种情况下,可以安全地抑制消息。

根据这篇文章,这是一个已知的bug,因此应该保存以忽略错误。

I agree. 我同意。 This makes no sense - the signal will not survive undisposed. 这没有任何意义 - 信号不会无法生存。 Looks to me like an error in the parser (for the condition). 在我看来像解析器中的错误(对于条件)。 I would document it and put a pgragma into the file to supporess it. 我会记录它并在文件中放入一个pgragma来支持它。

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

相关问题 CA1001在异步方法上实现IDisposable - CA1001 implement IDisposable on async method 警告CA1001实现IDisposable可忽略不计 - Is warning CA1001 implement IDisposable negligible CA1001报告了扩展类中的静态方法 - CA1001 reported on static method in extension class 内存异常; NullReference; CA1001实现IDisposable - Memory Exception; NullReference; CA1001 implement IDisposable WPF代码分析:CA1001具有一次性字段的类型应该是一次性的 - WPF code analyze : CA1001 Types that own disposable fields should be disposable CA1001 Visual Studio 2012代码分析警告。 这是什么意思? - CA1001 Visual Studio 2012 Code Analysis warning. What does it mean? 对不使用 IDisposable 的框架组件使用 .NET 分析器规则 CA1001 - Using .NET analyzer rule CA1001 for components of a framework which does not use IDisposable 错误消息:CA1001 - 错误消息:表单创建 IDiposable 类型 - Error Message: CA1001 - Error Message: Form Creates IDiposable Types CA1309对于针对.NET 4.5.1编译的源是否仍然有效? - Is CA1309 still valid for source compiled against .NET 4.5.1? CA2000错误,带有“ using”语句。 如何使其有效? - CA2000 error with 'using' statement. How to make this valid?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM