WeakReference 构造函数

定义

初始化 WeakReference 类的新实例。Initializes a new instance of the WeakReference class.

重载

WeakReference()
WeakReference(Object)

引用指定的对象初始化 WeakReference 类的新实例。Initializes a new instance of the WeakReference class, referencing the specified object.

WeakReference(Object, Boolean)

通过引用指定对象并使用指定的复活跟踪,初始化 WeakReference 类的新实例。Initializes a new instance of the WeakReference class, referencing the specified object and using the specified resurrection tracking.

WeakReference(SerializationInfo, StreamingContext)

使用从指定的序列化和流对象反序列化的数据,初始化 WeakReference 类的新实例。Initializes a new instance of the WeakReference class, using deserialized data from the specified serialization and stream objects.

WeakReference()

protected:
 WeakReference();
protected WeakReference ();
Protected Sub New ()

适用于

WeakReference(Object)

引用指定的对象初始化 WeakReference 类的新实例。Initializes a new instance of the WeakReference class, referencing the specified object.

public:
 WeakReference(System::Object ^ target);
public WeakReference (object target);
public WeakReference (object? target);
new WeakReference : obj -> WeakReference
Public Sub New (target As Object)

参数

target
Object

要跟踪的对象或为 nullThe object to track or null.

注解

此构造函数创建对的短弱引用 targetThis constructor creates a short weak reference to target.

另请参阅

适用于

WeakReference(Object, Boolean)

通过引用指定对象并使用指定的复活跟踪,初始化 WeakReference 类的新实例。Initializes a new instance of the WeakReference class, referencing the specified object and using the specified resurrection tracking.

public:
 WeakReference(System::Object ^ target, bool trackResurrection);
public WeakReference (object target, bool trackResurrection);
public WeakReference (object? target, bool trackResurrection);
new WeakReference : obj * bool -> WeakReference
Public Sub New (target As Object, trackResurrection As Boolean)

参数

target
Object

要跟踪的对象。An object to track.

trackResurrection
Boolean

指示何时停止跟踪对象。Indicates when to stop tracking the object. 若在完成后跟踪对象,则为 true;若为 false,表示仅在完成前跟踪对象。If true, the object is tracked after finalization; if false, the object is only tracked until finalization.

示例

下面的示例创建具有短弱引用的数据对象的缓存。The following example creates a cache of data objects with short weak references. 此示例是为类提供的更大示例的一部分 WeakReferenceThis example is part of a larger example provided for the WeakReference class.

 // Add objects with a short weak reference to the cache.
for (int i = 0; i < count; i++) {
     _cache.Add(i, new WeakReference(new Data(i), false));
 }
' Add data objects with a short weak reference to the cache. 
For ctr = 0 To count - 1
    _cache.Add(ctr, New WeakReference(New Data(ctr)))
Next

注解

如果 trackResurrectionfalse ,则会创建短弱引用。If trackResurrection is false, a short weak reference is created. 如果 trackResurrectiontrue ,则创建长弱引用。If trackResurrection is true, a long weak reference is created.

另请参阅

适用于

WeakReference(SerializationInfo, StreamingContext)

使用从指定的序列化和流对象反序列化的数据,初始化 WeakReference 类的新实例。Initializes a new instance of the WeakReference class, using deserialized data from the specified serialization and stream objects.

protected:
 WeakReference(System::Runtime::Serialization::SerializationInfo ^ info, System::Runtime::Serialization::StreamingContext context);
protected WeakReference (System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
new WeakReference : System.Runtime.Serialization.SerializationInfo * System.Runtime.Serialization.StreamingContext -> WeakReference
Protected Sub New (info As SerializationInfo, context As StreamingContext)

参数

info
SerializationInfo

保存序列化或反序列化当前的 WeakReference 对象所需的全部数据的对象。An object that holds all the data needed to serialize or deserialize the current WeakReference object.

context
StreamingContext

(保留)描述由 info 指定的序列化流的源和目标。(Reserved) Describes the source and destination of the serialized stream specified by info.

例外

infonullinfo is null.

注解

context参数是保留的,当前不参与此操作。The context parameter is reserved, and does not currently participate in this operation.

适用于