تمثل الفئة WeakReference مرجعا ضعيفا يؤشر إلى غرض Object مع إبقاء تمكين جامع النفايات من التعامل معه وهي موجودة ضمن مجال الأسماء System في المجمع mscorlib.dll والتصريح عنها يأتي كما يلي
<SerializableAttribute> <ComVisibleAttribute(True)> _ <SecurityPermissionAttribute(SecurityAction.InheritanceDemand, Flags := SecurityPermissionFlag.UnmanagedCode)> _ Public Class WeakReference _ Implements ISerializable
واستخدامها
Dim instance As WeakReference
حيث يمكن هذا المرجع الضعيف جامع النفايات من الحصول على الغرض وفي نفس الوقت يبقي للتطبيق إمكانية استخدام ذلك الغرض وإن كنت تحتاج لذلك الغرض يمكنك الحصول على مرجع قوي له ومنعه من أن يتم جمعه
يرينا المثال التالي كيف يمكننا استخدام المرجع الضعيف للمحافظة على مخبأ للأغراض كمصادر للتطبيق حيث يتم إنشاء المخبأ باستخدام IDictionary(TKey, TValue) حيث يتم تصنيف الأغراض بفهرس من المفاتيح حيث تحتوي الخاصية Target لأغراض WeakReference على مصفوفة تمثل البيانات
Imports System
Imports System.Collections.Generic
Public Class Module1
Public Shared Sub Main()
' Create the cache.
Dim cacheSize As Integer = 50
Dim r As Random = New Random
Dim c As Cache = New Cache(cacheSize)
Dim DataName As String = ""
' Randomly access objects in the cache.
Dim i As Integer
Do While i < c.Count
Dim index As Integer = r.Next(c.Count)
' Access the object by
' getting a property value.
DataName = c(index).Name
i += 1
Loop
'Show results.
Dim regenPercent As Double = c.RegenerationCount * 100 / c.Count
Console.WriteLine("Cache size: {0}, Regenerated: {1}%", c.Count.ToString(), regenPercent.ToString())
End Sub
End Class
Public Class Cache
' Dictionary to contain the cache.
Private Shared _cache As Dictionary(Of Integer, WeakReference)
' Track the number of times an
' object is regenerated.
Dim regenCount As Integer = 0
Public Sub New(ByVal count As Integer)
MyBase.New()
_cache = New Dictionary(Of Integer, WeakReference)
' Add data objects with a short
' weak reference to the cache.
Dim i As Integer = 0
Do While (i < count)
_cache.Add(i, New WeakReference(New Data(i)))
i = (i + 1)
Loop
End Sub
' Number of items in the cache.
Public ReadOnly Property Count() As Integer
Get
Return _cache.Count
End Get
End Property
' Number of times an
' object needs to be regenerated.
Public ReadOnly Property RegenerationCount() As Integer
Get
Return regenCount
End Get
End Property
' Access a data object from the cache.
' If the object was reclaimed for garbage collection,
' create a new data object at that index location.
Default Public ReadOnly Property Item(ByVal index As Integer) As Data
Get
' Obtain an instance of a data
' object from the cache of
' of weak reference objects.
Dim d As Data = CType(_cache(index).Target, Data)
If (d Is Nothing) Then
' Object was reclaimed, so generate a new one.
Console.WriteLine("Regenerate object at {0}: Yes", index.ToString())
d = New Data(index)
regenCount += 1
Else
' Object was obtained with the weak reference.
Console.WriteLine("Regenerate object at {0}: No", index.ToString())
End If
Return d
End Get
End Property
End Class
' Class that creates byte arrays to simulate data.
Public Class Data
Private _data() As Byte
Private _name As String
Public Sub New(ByVal size As Integer)
MyBase.New()
_data = New Byte(((size * 1024)) - 1) {}
_name = size.ToString
End Sub
' Simple property for
' accessing the object.
Public ReadOnly Property Name() As String
Get
Return _name
End Get
End Property
End Class
' Example of the last lines of the output:
' ...
' Regenerate object at 36: Yes
' Regenerate object at 8: Yes
' Regenerate object at 21: Yes
' Regenerate object at 4: Yes
' Regenerate object at 38: No
' Regenerate object at 7: Yes
' Regenerate object at 2: Yes
' Regenerate object at 43: Yes
' Regenerate object at 38: No
' Cache size: 50, Regenerated: 94%
'