处理处理IDisposable对象的通用函数 [英] Generic function to handle disposing IDisposable objects

查看:93
本文介绍了处理处理IDisposable对象的通用函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在处理一个处理很多Sql对象的类 - Connection,Command,DataAdapter,CommandBuilder等。有多个实例代码是这样的:

  if(command!= null)
{
command.Dispose();


if(dataAdapter!= null)
{
dataAdapter.Dispose();
}


我知道这是相当不足的重复条款,但它已开始闻起来。我认为它闻起来的原因是因为在某些情况下,对象也设置为null。

  if(command!= null )
{
command.Dispose();
command = null;
}

如果可能,我希望摆脱重复。

  private void DisposeObject< TDisposable>(() (TDisposableDispostingObject)
其中TDisposable:class,IDisposable
{
if(disposableObject!= null)
{
disposableObject.Dispose();
disposableObject = null;


我的问题是...


  1. 这个通用函数是一个坏主意吗?

  2. 是否需要将对象设置为 null

编辑:

I我知道使用语句,但是我不能总是使用它,因为我有一些成员变量需要持续一个以上的调用。例如连接和事务对象。



谢谢!

解决方案

我假设这些是字段而不是局部变量,因此使用关键字没有意义。


这个泛型函数是一个坏主意吗?

我认为这是一个好主意,我已经使用了类似的功能几次; +
$ b


是否需要将对象设置为空?




从技术上讲,一个对象应该允许多次调用它的 Dispose 方法。 (例如,如果一个对象在定稿过程中复活,就会发生这种情况。)在实践中,是否信任这些类的作者或是否想防守编码取决于您。我个人会检查null,然后将引用设置为null。



编辑:如果此代码位于您自己的对象的 Dispose 方法,则无法将引用设置为null将不会泄漏内存。相反,它可以很好地防止双重处置。


I am working on a class that deals with a lot of Sql objects - Connection, Command, DataAdapter, CommandBuilder, etc. There are multiple instances where we have code like this:

if( command != null )
{
    command.Dispose();
}

if( dataAdapter != null )
{
    dataAdapter.Dispose();
}

etc

I know this is fairly insufficient in terms of duplication, but it has started smelling. The reason why I think it smells is because in some instances the object is also set to null.

if( command != null )
{
    command.Dispose();
    command = null;
}

I would love to get rid of the duplication if possible. I have come up with this generic method to dispose of an object and set it to null.

private void DisposeObject<TDisposable>( ref TDisposable disposableObject )
    where TDisposable : class, IDisposable
{
    if( disposableObject != null )
    {
        disposableObject.Dispose();
        disposableObject = null;
    }
}

My questions are...

  1. Is this generic function a bad idea?
  2. Is it necessary to set the object to null?

EDIT:

I am aware of the using statement, however I cannot always use it because I have some member variables that need to persist longer than one call. For example the connection and transaction objects.

Thanks!

解决方案

I assume these are fields and not local variables, hence why the using keyword doesn't make sense.

Is this generic function a bad idea?

I think it's a good idea, and I've used a similar function a few times; +1 for making it generic.

Is it necessary to set the object to null?

Technically an object should allow multiple calls to its Dispose method. (For instance, this happens if an object is resurrected during finalization.) In practice, it's up to you whether you trust the authors of these classes or whether you want to code defensively. Personally, I check for null, then set references to null afterwards.

Edit: If this code is inside your own object's Dispose method then failing to set references to null won't leak memory. Instead, it's handy as a defence against double disposal.

这篇关于处理处理IDisposable对象的通用函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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