![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
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);
}
}
}
{
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);
}
}
}