WeakReferenceTable<TKey, TValue> Class
Namespace: PTS.Concurrency
Assembly: PTS.Concurrency.dll
Enables compilers to dynamically attach weak references of objects as dynamic fields to managed objects.
Syntax
public class WeakReferenceTable<TKey, TValue>
where TKey : class where TValue : class
Type Parameters
Name | Description |
---|---|
TKey | The reference type to which the field is attached. |
TValue | The type of a field that will be attached as weak reference. |
Inheritance
Remarks
Works similarly to System.Runtime.CompilerServices.ConditionalWeakTable`2 with the properties wrapped in System.WeakReference`1 to let garbage collection of a key object to occur.
Methods
Add(TKey, TValue)
Adds a key to the table.
Declaration
public void Add(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to add. key represents the object to which the property is attached. |
TValue | value | Value to be assigned to the key as a typed weak reference, allowing the value to be reclaimed by garbage collection. |
AddOrUpdate(TKey, TValue)
Adds a key to the table if the key was not already assigned or updates the value of existing key with the value from the parameter.
Declaration
public void AddOrUpdate(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to add or update. key represents the object to which the property is attached. |
TValue | value | Value to be assigned to the key as a typed weak reference, allowing the value to be reclaimed by garbage collection. |
GetValue(TKey)
Retrieves the target property that is referenced by the current object.
Declaration
public TValue GetValue(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key that represents an object with an attached property. |
Returns
Type | Description |
---|---|
TValue | Attached property value if found or is still avaialble; otherwise the default value. |
Remove(TKey)
Removes a key and its value from the table.
Declaration
public bool Remove(TKey key)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to remove. |
Returns
Type | Description |
---|---|
System.Boolean | Returns |
TryAdd(TKey, TValue)
Adds a key to the table if the key was not already assigned.
Declaration
public bool TryAdd(TKey key, TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key to add. key represents the object to which the property is attached. |
TValue | value | Value to be assigned to the key as a typed weak reference, allowing the value to be reclaimed by garbage collection. |
Returns
Type | Description |
---|---|
System.Boolean | Returns |
TryGetValue(TKey, out TValue)
Tries to retrieve the target property that is referenced by the current object.
Declaration
public bool TryGetValue(TKey key, out TValue value)
Parameters
Type | Name | Description |
---|---|---|
TKey | key | The key that represents an object with an attached property. |
TValue | value | When this method returns, contains the target property, if it is available. This parameter is treated as uninitialized. |
Returns
Type | Description |
---|---|
System.Boolean | true if key is found; otherwise, false. |