我们可以/是否需要-在aspx.cs类中实现IDisposable? [英] Can we/Do we need to - implement IDisposable in aspx.cs class?

查看:45
本文介绍了我们可以/是否需要-在aspx.cs类中实现IDisposable?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

公共局部类SelectForm:BasePage,IDisposable
{

public partial class SelectForm : BasePage,IDisposable
{

public override void Dispose()
{
    Dispose(true);
    GC.SuppressFinalize(this);
}





protected virtual void Dispose(bool disposing)
{
    if (!disposed)
    {
        if (disposing)
        {
            #region Objects which are getting disposed
            if (_fieldControlHash != null)
            {
                _fieldControlHash = null;
            }
            if (_log != null)
            {
                _log = null;
            }
            if (_tLogic != null)
            {
                _tLogic = null;
            }
            if (_curTrans != null)
            {
                _curTrans=null;
            }
            if (_curUser != null)
            {
                _curUser=null;
            }
            if (_dynpop != null)
            {
                _dynpop = null;
            }
            //if (_environ != null)
            //{
            //    _environ = null;
            //}
            if (conGenGlobal != null)
            {
                conGenGlobal = null;
            }
            if (_CMSNotesLogic != null)
            {
                _CMSNotesLogic = null;
            }
            if (el != null)
            {
                el = null;
            }
            if (elog != null)
            {
                elog = null;
            }
            if (_mlogger != null)
            {
                _mlogger = null;
            }
            if (_mlog != null)
            {
                _mlog = null;
            }

            #endregion
        }
        disposed = true;
    }
}




}




}

推荐答案

仅在需要处理资源时才需要实现IDisposable .通常,如果您使用实现IDisposible的类,则需要确保在使用完该类后,执行该类的Dispose方法.这可以通过使用using 语句或在try...catch...finallyfinally 部分中调用Dispose来完成.否则,您应该实现IDisposable,并确保处置Disposable资源.您还需要确保在完成该类后调用Dispose 方法,否则没有意义.您可以确保doin调用了上述类的Dispose 方法.您还可以创建一些案例,在这些案例中,您管理的资源不是一次性的,但在业务应用程序中很少见.
You only need to implement IDisposable if you need to dispose of resources. Generally, if you are using a class that implements IDisposible, you need to ensure that the class''s Dispose method is executed when you are finished using the class. This can be done by using the using statement, or by calling Dispose in the finally part of a try...catch...finally. Otherwise you should implement IDisposable, and ensure that the Disposable resource is disposed of. You also need to ensure that you call the Dispose method when finished with the class, otherwise there is no point. You can ensure that a class''s Dispose method is called by doin what was stated above. You can also create cases where you are managing resources that do not have a disposable, but that is rare in business applications.


这篇关于我们可以/是否需要-在aspx.cs类中实现IDisposable?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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