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);
                }
            }
        }
  • 相关阅读:
    把Orchard部署到Windows Azure Web Sites
    使用Windows Live Writer 发布博客园博客
    使用Microsoft Word 2013 发布Blog到博客园
    Java栈的简单实现
    Java中的运算符
    Java简单双向链表实现 @version 1.0
    Java中的面向对象II
    认识和分析日志文件
    两数之和问题
    括号序列算法
  • 原文地址:https://www.cnblogs.com/sofire/p/1754907.html
Copyright © 2011-2022 走看看