在处理类实例时,我是否需要明确地处理其所有IDisposable成员? [英] While disposing the class instance, do i need to dispose all its IDisposable members explicitly?

查看:125
本文介绍了在处理类实例时,我是否需要明确地处理其所有IDisposable成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个具有 SqlConnection 类型属性的类。 SqlConnection 实现 IDisposable 。我有以下问题:


  1. 我的类是否也应该实现 IDisposable 属性类型 IDisposable

  2. 如果是,我是否需要在处理我的实例时明确地配置属性类?例如:

      public class Helper:IDisposable 
    {
    //假设它是任何其他IDisposable类型。 SqlConnection只是一个例子。
    public SqlConnection SqlConnection {get;组; }
    $ b $ public void Dispose()
    {
    if(SqlConnection!= null)
    {
    SqlConnection.Dispose();







    注意:
    我知道在执行 IDisposable 时有一个模式需要遵循,但我的问题与上述情况非常具体。

    解决方案

    取决于。如果你的类创建了和拥有 IDisposable ,那么它必须处理它(所以,答案是)。
    如果你的类只是使用 IDisposable 它不能处理它(所以第一个答案通常是,,第二个答案是)。 助手

     公共类助手
    {
    //注意,你可以通过这个属性赋值任意的连接
    public SqlConnection SqlConnection {get;组; }
    ....
    }

    just uses em> SqlConnection (因为它提供了set),就像

     <$ c $ (SqlConnection con = new SqlConnection(...)){
    ...
    // helper只是使用Connection,所以helper一定不能使用SqlConnection
    处置它
    助手助手=新助手(){
    SqlConnection = con;
    };

    ...
    }

    所以它不得处理连接。相反,像这样的类

      public class Helper:IDisposable {
    private SqlConnection m_SqlConnection;

    //注意缺少公共set
    public SqlConnection SqlConnection {
    get {
    return m_SqlConnection;
    }
    }
    ...
    }

    拥有它的 SqlConnection ,所以它负责处理它:

      using(Helper helper = new Helper(...)){
    ...
    //它是拥有SqlConnection的助手
    SqlConnection con = helper.SqlConnection;
    ...
    }


    I have a class which has a property of the type SqlConnection. SqlConnection implements IDisposable. I have following questions:

    1. Should my class also implement IDisposable just because it has property of type IDisposable?
    2. If yes, do i need to Dispose the property explicitly when I am disposing instance of my class? E.g.

      public class Helper : IDisposable
      {
          // Assume that it's ANY OTHER IDisposable type. SqlConnection is just an example.
          public SqlConnection SqlConnection { get; set; }
      
          public void Dispose()
          {
              if (SqlConnection!= null)
              {
                  SqlConnection.Dispose();
              }
          }
      }
      

    Note : I know that there is a pattern to be followed while implementing IDisposable but my question is very specific to the case mentioned above.

    解决方案

    It depends. If your class creates and owns the IDisposable it must dispose it (so, both answers are "yes"). If your class just uses IDisposable it must not dispose it (so the first answer is, usually, "no" and the second answer is "no").

    In your case, it seems that Helper class

      public class Helper
      {
          // Note, that you can assign any arbitrary Connection via this property
          public SqlConnection SqlConnection { get; set; }
          ....
      }
    

    just uses SqlConnection (because it provides "set") in the way like

    // It's not helper that owns SqlConnection
    using (SqlConnection con = new SqlConnection(...)) {
      ...
      // helper just uses Connection, so helper must not dispose it
      Helper helper = new Helper() {
        SqlConnection = con; 
      };
    
      ...
    }
    

    so it must not dispose the connection. On the contrary, a class like that

    public class Helper: IDisposable {
      private SqlConnection m_SqlConnection;
    
      // Note the absence of public "set"
      public SqlConnection SqlConnection {
        get {
          return m_SqlConnection; 
        } 
      }
      ...
    }  
    

    owns its SqlConnection so it's responsible for disposing it:

    using (Helper helper = new Helper(...)) {
      ...
      // it's helper that owns SqlConnection
      SqlConnection con = helper.SqlConnection;
      ...
    } 
    

    这篇关于在处理类实例时,我是否需要明确地处理其所有IDisposable成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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