zoukankan      html  css  js  c++  java
  • [转载].NET Tip: Implement IDisposable to Ensure Your Objects Clean Up After Themselves Virus

    public class GoodResourceCitizen : IDisposable
    {
        private bool _IsDisposed = false;
        ~GoodResourceCitizen()
        {
            Dispose(false);
        }
        public void Dispose()
        {
            Dispose(true);
            // Tell the garbage collector not to call the finalizer
            // since all the cleanup will already be done.
            GC.SuppressFinalize(true);
        }
        protected virtual void Dispose(bool IsDisposing)
        {
            if (_IsDisposed)
              return;
           if (IsDisposing)
           {
               // Free any managed resources in this section
           }
           // Free any unmanaged resources in this section
           _IsDisposed = true;
        }
    }
     public class Person:IDisposable
        {
            public int Age { get; set; }
    
            public override string ToString()
            {
    
                return base.ToString();
            }
            public Person ShallowClone()
            {
    
                return base.MemberwiseClone() as Person;
            }
    
            public Person DeepClone()
            {
                var p = new Person();
                p.Age = this.Age;
                p.Address = new Address() { Detail = this.Address.Detail };
                return p;
    
            }
            /// <summary>
            /// 析构函数
            /// 自动调用基类的Finalize()释放非托管资源
            /// </summary>
            ~Person()
            {
                //会自动调用base.Finalize();用来释放非托管资源
                Dispose(false);
            }
    
    
            public Address Address { get; set; }
    
            public void Dispose()
            {
                Dispose(true);
                //在上面的Dispose(true)已经释放了非托管资源,所以GC就不能在调用Finalize()方法了,所以需要废除当前对象的Finalize()
                GC.SuppressFinalize(this);
            }
            /// <summary>
            /// 
            /// </summary>
            /// <param name="disposing">是否Dispose方法调用自己</param>
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    //release managed resources
                }
                //release unmanaged resources
            }
            
        }
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace ConsoleApplication1
    {
        public class Human:IDisposable
        {
            ~Human()
            {
                Dispose(false);
         
            }
    
            public void Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    //release managed resources
                }
    
                //release unmanaged resources
    
            }
        }
    
        public class Animal 
        {
            ~Animal()
            {
                Dispose(false);
    
            }
    
            public void IDisposable.Dispose()
            {
                Dispose(true);
                GC.SuppressFinalize(this);
            }
            protected virtual void Dispose(bool disposing)
            {
                if (disposing)
                {
                    //release managed resources
                }
    
                //release unmanaged resources
    
            }
            public void Close()
            {
                Dispose(false);
            }
        }
    
        public class demo
        {
            public void haha()
            {
                var human = new Human();
                human.Dispose();
    
                var animal = new Animal();
                animal.Close();
    
                (animal as IDisposable).Dispose();
            }
        }
    }

    参考文献

    1.C#中Finalize方法的问题

    2.Object.Finalize 方法

    3.C#中Finalize方法的问题

    4.C#中对象的销毁有三种方式Finalize,Dispose,GC。

    5.《Effective C#》读书笔记——了解.NET内存管理机制<.NET资源管理>

    【Blog】http://virusswb.cnblogs.com/

    【MSN】jorden008@hotmail.com

    【说明】转载请标明出处,谢谢

    反馈文章质量,你可以通过快速通道评论:

  • 相关阅读:
    Python 购物车程序(文件版)
    Python 购物车程序
    Python多级菜单显示和登录小接口
    ARM体系结构与编程-第五章
    ARM体系结构与编程-第四章
    ARM的IRQ模式和FIQ模式
    C结构体的初始化和赋值
    ARM体系结构与编程-第三章
    函数调用过程分析
    关于STM32-M3/M4的MSP和PSP
  • 原文地址:https://www.cnblogs.com/virusswb/p/868144.html
Copyright © 2011-2022 走看看