// No finalizer, because we are only disposing WeakTable. Its own finalizer will take care of the reference if // necessary. This override just serves to allow the table to be explicitly disposed. public override void Dispose() { if (WeakTable != null) { WeakTable.Dispose(); WeakTable = null; } }
public void WeakTableNoValue() { var table = new WeakTable <BaseObject, string>(); var obj1 = new BaseObject(); Assert.IsFalse(table.HasValue(obj1)); Assert.IsNull(table.GetValue(obj1)); Assert.IsFalse(table.TryGetValue(obj1, out _)); }
public void WeakTableIndexer() { var table = new WeakTable <BaseObject, string>(); var obj1 = new BaseObject(); table[obj1] = "obj1"; Assert.IsTrue(table.HasValue(obj1)); Assert.AreEqual("obj1", table[obj1]); }
public void WeakTableKeyIsNull() { var table = new WeakTable <BaseObject, string>(); Assert.IsFalse(table.HasValue(null)); Assert.IsNull(table.GetValue(null)); Assert.IsFalse(table.TryGetValue(null, out _)); table.SetValue(null, "obj0"); string s; Assert.IsTrue(table.TryGetValue(null, out s)); Assert.AreEqual("obj0", s); }
public void WeakTableStorage() { var table = new WeakTable <BaseObject, string>(); var obj1 = new BaseObject(); table.SetValue(obj1, "obj1"); Assert.IsTrue(table.HasValue(obj1)); Assert.AreEqual("obj1", table.GetValue(obj1)); string s; Assert.IsTrue(table.TryGetValue(obj1, out s)); Assert.AreEqual("obj1", s); }
/// <summary> /// Get an associate for a particular object /// </summary> /// <param name='reference'> /// The object whose associate should be found /// </param> /// <param name='create'> /// Whether the associate should be created (defaults true) /// </param> /// <typeparam name='T'> /// The type of associate /// </typeparam> public static T Get <T>(this object reference, bool create = true) where T : class, new() { WeakTable <object> references; //Try to get a weaktable for the reference object if (!extensions.TryGetValue(reference.GetType(), out references)) { //Verify that we should be creating it if missing if (!create) { return(null); } //Create a new table extensions[reference.GetType()] = references = new WeakTable <object>(); } //Get the associate from the table return((T)references.Get <T>(reference)); }
public void WeakTableForget() { var table = new WeakTable <BaseObject, string>(); WeakReference weakref; void _WorkWithObj1() { var obj1 = new BaseObject(); table[obj1] = "obj1"; Assert.IsTrue(table.HasValue(obj1)); Assert.AreEqual("obj1", table[obj1]); weakref = new WeakReference(obj1); obj1 = null; } _WorkWithObj1(); GC.Collect(); Assert.IsFalse(table.HasValue(weakref.Target as BaseObject)); Assert.IsNull(table.GetValue(weakref.Target as BaseObject)); }
protected override LuaValue CopyReferenceImpl() { return(new LuaWeakReference <T>((LuaTable)WeakTable.CopyReference())); }