Exemplo n.º 1
0
        public override bool Equals(object obj)
        {
            if (this == obj)
            {
                return(true);
            }

            WeakRefWrapper that = obj as WeakRefWrapper;

            if (that == null)
            {
                return(false);
            }

            object target = Target;

            if (target == null)
            {
                // If the reference is dead, we are only equal to ourselves,
                // and this was checked above.
                return(false);
            }

            return(hashCode == that.hashCode &&
                   Equals(target, that.Target));
        }
Exemplo n.º 2
0
 public object this[object key]
 {
     get { return(WeakRefWrapper.Unwrap(innerHashtable[WeakRefWrapper.Wrap(key)])); }
     set
     {
         Scavenge();
         innerHashtable[WeakRefWrapper.Wrap(key)] = WeakRefWrapper.Wrap(value);
     }
 }
		public void WeakReferenceGetsFreedButHashCodeRemainsConstant()
		{
			object obj = new object();
			WeakRefWrapper wr = new WeakRefWrapper(obj);
			int hashCode = wr.GetHashCode();
			obj = null;

			GC.Collect();

			Assert.IsFalse(wr.IsAlive);
			Assert.IsNull(wr.Target);
			Assert.AreEqual(hashCode, wr.GetHashCode());
		}
Exemplo n.º 4
0
        public void Scavenge()
        {
            ArrayList deadKeys = new ArrayList();

            foreach (DictionaryEntry de in innerHashtable)
            {
                WeakRefWrapper key   = ( WeakRefWrapper )de.Key;
                WeakRefWrapper value = ( WeakRefWrapper )de.Value;

                if (!key.IsAlive || !value.IsAlive)
                {
                    deadKeys.Add(key);
                }
            }

            foreach (object key in deadKeys)
            {
                innerHashtable.Remove(key);
            }
        }
Exemplo n.º 5
0
        public bool MoveNext()
        {
            // Skip any garbage-collected key/value pairs
            while (true)
            {
                currentKey   = null;
                currentValue = null;

                if (!innerEnumerator.MoveNext())
                {
                    return(false);
                }

                currentKey   = WeakRefWrapper.Unwrap(innerEnumerator.Key);
                currentValue = WeakRefWrapper.Unwrap(innerEnumerator.Value);

                if (currentKey != null && currentValue != null)
                {
                    break;
                }
            }

            return(true);
        }
Exemplo n.º 6
0
 public void Remove(object key)
 {
     innerHashtable.Remove(WeakRefWrapper.Wrap(key));
 }
Exemplo n.º 7
0
 public void Add(object key, object value)
 {
     Scavenge();
     innerHashtable.Add(WeakRefWrapper.Wrap(key), WeakRefWrapper.Wrap(value));
 }
Exemplo n.º 8
0
 public bool Contains(object key)
 {
     return(innerHashtable.Contains(WeakRefWrapper.Wrap(key)));
 }