zoukankan      html  css  js  c++  java
  • 泛型弱引用(不继承 System.WeakReference)

    代码
    public class WeakReference<T> : IDisposable
        {
            
    private GCHandle handle;
            
    private bool trackResurrection;

            
    public WeakReference(T target) : this(target, false) { }

            
    public WeakReference(T target, bool trackResurrection)
            {
                
    this.trackResurrection = trackResurrection;
                
    this.Target = target;
            }

            
    ~WeakReference()
            {
                Dispose();
            }

            
    public void Dispose()
            {
                handle.Free();
                GC.SuppressFinalize(
    this);
            }

            
    public virtual bool IsAlive
            {
                
    get { return (handle.Target != null); }
            }

            
    public virtual bool TrackResurrection
            {
                
    get { return this.trackResurrection; }
            }

            
    public virtual T Target
            {
                
    get
                {
                    
    object o = handle.Target;
                    
    if ((o == null|| (!(o is T)))
                        
    return default(T);
                    
    else
                        
    return (T)o;
                }
                
    set
                {
                    handle 
    = GCHandle.Alloc(value, this.trackResurrection ? GCHandleType.WeakTrackResurrection : GCHandleType.Weak);
                }
            }
        }
  • 相关阅读:
    linux上mysql安装详细教程
    druid部署
    druid.io本地集群搭建 / 扩展集群搭建
    CentOS7.1.x+Druid 0.12 集群配置
    Python小项目四:实现简单的web服务器
    hadoop学习---运行第一个hadoop实例
    Hadoop集群完全分布式坏境搭建
    mysql备份与恢复
    mysql登录的三种方式
    nginx代理,负载均衡
  • 原文地址:https://www.cnblogs.com/sofire/p/1754907.html
Copyright © 2011-2022 走看看