32 lines
707 B
C#
32 lines
707 B
C#
|
using System;
|
|||
|
namespace ln.types
|
|||
|
{
|
|||
|
class WeakHashValue<T> where T:class
|
|||
|
{
|
|||
|
readonly int keyHashCode;
|
|||
|
|
|||
|
WeakReference<T> reference;
|
|||
|
|
|||
|
public T Value => reference.TryGetTarget(out T target) ? target : null;
|
|||
|
public bool IsStrong => reference.TryGetTarget(out T target);
|
|||
|
|
|||
|
public WeakHashValue(T value)
|
|||
|
{
|
|||
|
reference = new WeakReference<T>(value);
|
|||
|
keyHashCode = value.GetHashCode();
|
|||
|
}
|
|||
|
|
|||
|
public override int GetHashCode() => keyHashCode;
|
|||
|
public override bool Equals(object obj)
|
|||
|
{
|
|||
|
T value = Value;
|
|||
|
if ((value != null) && (obj is WeakHashValue<T>))
|
|||
|
{
|
|||
|
WeakHashValue<T> other = obj as WeakHashValue<T>;
|
|||
|
return object.Equals(value,other.Value);
|
|||
|
}
|
|||
|
return false;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|