如果初始化失败,如何将IDisposable对象传递给在C#中处理的基础构造函数参数? [英] How are IDisposable objects passed to base constructor arguments handled in C# if initialization fails?

查看:158
本文介绍了如果初始化失败,如何将IDisposable对象传递给在C#中处理的基础构造函数参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我使用基地(...)结构传递一个IDisposable的对象基类的构造函数,我引发的在FxCop的傻错误了解让松散的 IDisposable的。该警告是偶尔有用其他地方,所以我不希望禁用它,但我知道我不知道我应该使用精确语义。

If I pass an IDisposable object to a base class constructor using the base(...) construct, I trigger a silly error in FxCop about letting loose of an IDisposable. The warning is occasionally useful elsewhere, so I don't want to disable it, but I realize I don't know the exact semantics I should be using.

这是基构造的责任来包装它的身体在一个try / catch以确保的IDisposable 的是妥善处置异常的情况?

Is it the base constructor's responsibility to wrap its body in a try/catch to ensure the IDisposable thing is disposed of properly in case of exception?

相关问题:

  • CA2000 passing object reference to base constructor in C#
  • Passing IDisposable objects through constructor chains

推荐答案

不是一个真正的问题的答案,但是...

Not really an answer to the question, but...

您不一定需要禁用规则共 - 可以抑制它的方法,你知道的分析是过于热心的:

You don't necessarily need to disable the rule altogether - you can suppress it for the methods where you know that the analysis is over-zealous:

[SuppressMessage("Microsoft.Reliability",
                 "CA2000:DisposeObjectsBeforeLosingScope",
                 Justification = "Your reasons go here")]
public YourClass(IDisposable obj) : base(obj)
{
}

虽然CA2000分析的这样的破裂,这可能是更有用完全禁用它。

Although the CA2000 analysis is so broken that it might be more useful to disable it altogether.

这篇关于如果初始化失败,如何将IDisposable对象传递给在C#中处理的基础构造函数参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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