WeakReference::Target Property
Gets or sets the object (the target) referenced by the current WeakReference object.
Assembly: mscorlib (in mscorlib.dll)
public: property Object^ Target { virtual Object^ get(); virtual void set(Object^ value); }
Property Value
Type: System::Object^null if the object referenced by the current WeakReference object has been garbage collected; otherwise, a reference to the object referenced by the current WeakReference object.
| Exception | Condition |
|---|---|
| InvalidOperationException | The reference to the target object is invalid. This exception can be thrown while setting this property if the value is a null reference or if the object has been finalized during the set operation. |
After setting this property to the target object, make sure that there are no other strong references to the object; otherwise, it will not be collected.
The following example tries to obtain an object from a cache of objects with weak references. If the object was reclaimed for garbage collection, a new object is generated. This example is part of a larger example provided for the WeakReference class.
Available since 8
.NET Framework
Available since 1.1
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1