Last active
August 29, 2015 14:00
-
-
Save michaelbartnett/11269404 to your computer and use it in GitHub Desktop.
HashableWeakRef<T> examle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace TestHashableWeakRef | |
{ | |
class MainClass | |
{ | |
public static void Main (string[] args) | |
{ | |
object keyObj = new object(); | |
var dict = new Dictionary<HashableWeakRef<object>, string>(); | |
var weakKey = new HashableWeakRef<object>(keyObj); | |
Console.WriteLine("Setting dict[weakKey]..."); | |
dict[weakKey] = "value in the dict"; | |
PrintDict(dict); | |
var anotherWeakKey = new HashableWeakRef<object>(keyObj); | |
Console.WriteLine("Setting dict[anotherWeakKey]..."); | |
dict[anotherWeakKey] = "a different value in the dict"; | |
PrintDict(dict); | |
Console.WriteLine("Unsetting dict[weakKey]..."); | |
dict.Remove(weakKey); | |
PrintDict(dict); | |
Console.WriteLine("Setting dict[anotherWeakKey]..."); | |
dict[anotherWeakKey] = "third value"; | |
PrintDict(dict); | |
Console.WriteLine("Setting dict[weakKey]..."); | |
dict[weakKey] = "final value"; | |
PrintDict(dict); | |
} | |
public static void PrintDict(Dictionary<HashableWeakRef<object>, string> dict) | |
{ | |
Console.WriteLine("Printing dictionary:\n{"); | |
foreach (var key in dict.Keys) { | |
Console.WriteLine(string.Format("\tKEY: {0} VALUE: {1} WEAKREFHASH: {2}", key, dict[key], key.GetWeakReferenceHashCode())); | |
} | |
Console.WriteLine("}\n"); | |
} | |
} | |
} | |
class HashableWeakRef<T> : IEquatable<HashableWeakRef<T>> where T : class | |
{ | |
private WeakReference weakRef; | |
public T Target { get { return (T) weakRef.Target; } } | |
public HashableWeakRef(T target) | |
{ | |
weakRef = new WeakReference(target); | |
} | |
public int GetWeakReferenceHashCode() | |
{ | |
return this.weakRef.GetHashCode(); | |
} | |
public override int GetHashCode() | |
{ | |
var tmpTarget = weakRef.Target; | |
if (tmpTarget == null) { | |
throw new InvalidOperationException("Can't hash a null weak ref"); | |
} | |
return tmpTarget.GetHashCode(); | |
} | |
public override bool Equals(object other) | |
{ | |
if (other is HashableWeakRef<T>) { | |
return this.Equals((HashableWeakRef<T>) other); | |
} | |
return false; | |
} | |
public bool Equals(HashableWeakRef<T> other) | |
{ | |
var tmpOtherTarget = other.weakRef.Target; | |
var tmpThisTarget = this.weakRef.Target; | |
if (tmpOtherTarget == null || tmpThisTarget == null) { | |
throw new ArgumentNullException("Can't compare null weakrefs"); | |
} | |
return tmpThisTarget.Equals(tmpOtherTarget); | |
} | |
public static bool operator ==(HashableWeakRef<T> a, HashableWeakRef<T> b) | |
{ | |
var tmpTargetA = (T) a.weakRef.Target; | |
var tmpTargetB = (T) b.weakRef.Target; | |
if (tmpTargetA == null || tmpTargetB == null) { | |
throw new ArgumentNullException("Can't test equality for null weakrefs"); | |
} | |
return tmpTargetA == tmpTargetB; | |
} | |
public static bool operator !=(HashableWeakRef<T> a, HashableWeakRef<T> b) | |
{ | |
return !(a == b); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment