T
CSharpTest.Net
WeakReference<T> Class
Members  Example  See Also  Send Feedback Download Help File
CSharpTest.Net.Library Assembly > CSharpTest.Net.Utils Namespace : WeakReference<T> Class

Glossary Item Box

A strong-typed derivation of the WeakReference class

Syntax

Visual Basic (Declaration) 
<SerializableAttribute()>
Public Class WeakReference(Of T As Class) 
   Inherits System.WeakReference
C# 
[SerializableAttribute()]
public class WeakReference<T> : System.WeakReference 
where T: class

Type Parameters

T

Example

Library/Library.Test/TestWeakReferenceT.cs

C#Copy Code
string value1 = "Testing Value - 1";
object value2 = new MyObject();
Utils.WeakReference<string> r = new Utils.WeakReference<string>(value1);

string tmp;
Assert.IsTrue(r.TryGetTarget(out tmp) && tmp == value1);

((WeakReference)r).Target = value2; //incorrect type...
Assert.IsFalse(r.IsAlive);
Assert.IsNull(r.Target);
Assert.IsFalse(r.TryGetTarget(out tmp));

Assert.IsTrue(ReferenceEquals(value2, ((WeakReference)r).Target));
VB.NETCopy Code
Dim value1 As String = "Testing Value - 1"
Dim value2 As Object = New MyObject()
Dim r As New Utils.WeakReference(Of String)(value1)

Dim tmp As String
Assert.IsTrue(r.TryGetTarget(tmp) AndAlso tmp = value1)

(DirectCast(r, WeakReference)).Target = value2
'incorrect type...
Assert.IsFalse(r.IsAlive)
Assert.IsNull(r.Target)
Assert.IsFalse(r.TryGetTarget(tmp))

Assert.IsTrue(ReferenceEquals(value2, (DirectCast(r, WeakReference)).Target))

Inheritance Hierarchy

System.Object
   System.WeakReference
      CSharpTest.Net.Utils.WeakReference<T>

Requirements

Target Platforms: Windows XP, Windows Server 2003, Windows Vista, Windows Server 2008, Windows 7

See Also

Generated with Document! X 2011 by Innovasys