1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
|
// ==++==
//
// Copyright (c) Microsoft Corporation. All rights reserved.
//
// ==--==
/*============================================================
**
** Class: WeakReference
**
** Purpose: A wrapper for establishing a WeakReference to an Object.
**
===========================================================*/
namespace System {
using System;
using System.Runtime.Serialization;
using System.Security;
using System.Security.Permissions;
using System.Runtime.CompilerServices;
using System.Runtime.Versioning;
using System.Diagnostics.Contracts;
[System.Runtime.InteropServices.ComVisible(true)]
#if !FEATURE_CORECLR
[SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags=SecurityPermissionFlag.UnmanagedCode)] // Don't call Object::MemberwiseClone.
#endif
[Serializable]
public class WeakReference : ISerializable {
// If you fix bugs here, please fix them in WeakReference<T> at the same time.
// This field is not a regular GC handle. It can have a special values that are used to prevent ----s between setting the target and finalization.
internal IntPtr m_handle;
#if FEATURE_CORECLR
// Migrating InheritanceDemands requires this default ctor, so we can mark it SafeCritical
[SecuritySafeCritical]
protected WeakReference() {
Contract.Assert(false, "WeakReference's protected default ctor should never be used!");
throw new NotImplementedException();
}
#endif
// Creates a new WeakReference that keeps track of target.
// Assumes a Short Weak Reference (ie TrackResurrection is false.)
//
public WeakReference(Object target)
: this(target, false) {
}
//Creates a new WeakReference that keeps track of target.
//
public WeakReference(Object target, bool trackResurrection) {
Create(target, trackResurrection);
}
protected WeakReference(SerializationInfo info, StreamingContext context) {
if (info==null) {
throw new ArgumentNullException("info");
}
Contract.EndContractBlock();
Object target = info.GetValue("TrackedObject",typeof(Object));
bool trackResurrection = info.GetBoolean("TrackResurrection");
Create(target, trackResurrection);
}
//Determines whether or not this instance of WeakReference still refers to an object
//that has not been collected.
//
public extern virtual bool IsAlive {
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[SecuritySafeCritical]
get;
}
//Returns a boolean indicating whether or not we're tracking objects until they're collected (true)
//or just until they're finalized (false).
//
public virtual bool TrackResurrection {
// We need to call IsTrackResurrection non-virtually in GetObjectData, and so the virtual property cannot be FCall directly
get { return IsTrackResurrection(); }
}
//Gets the Object stored in the handle if it's accessible.
// Or sets it.
//
public extern virtual Object Target {
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[SecuritySafeCritical]
get;
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[SecuritySafeCritical]
set;
}
// Free all system resources associated with this reference.
//
// Note: The WeakReference finalizer is not actually run, but
// treated specially in gc.cpp's ScanForFinalization
// This is needed for subclasses deriving from WeakReference, however.
// Additionally, there may be some cases during shutdown when we run this finalizer.
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[SecuritySafeCritical]
extern ~WeakReference();
[SecurityCritical]
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info==null) {
throw new ArgumentNullException("info");
}
Contract.EndContractBlock();
info.AddValue("TrackedObject", Target, typeof(Object));
info.AddValue("TrackResurrection", IsTrackResurrection());
}
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[SecuritySafeCritical]
private extern void Create(Object target, bool trackResurrection);
[ResourceExposure(ResourceScope.None)]
[MethodImplAttribute(MethodImplOptions.InternalCall)]
[SecuritySafeCritical]
private extern bool IsTrackResurrection();
}
}
|