コード例 #1
0
 public WeakKeyReference(T key, WeakKeyComparer <T> comparer)
     : base(key)
 {
     // retain the object's hash code immediately so that even if the target is GC'ed we will be
     // able to find and remove the dead weak reference.
     HashCode = comparer.GetHashCode(key);
 }
コード例 #2
0
        /// <summary>
        /// The constructor with a specified comparer and a refresh interval.
        /// </summary>
        /// <param name="comparer">
        /// The IEqualityComparer used to determine equals of object of <typeparamref name="TKey"/>.
        /// </param>
        /// <param name="refreshInterval">
        /// The interval for calling RemoveCollectedEntries to refresh the dictionary.
        /// </param>
        public WeakDictionary(IEqualityComparer <TKey> comparer, int refreshInterval)
        {
            this.comparer = comparer as WeakKeyComparer <TKey>;
            if (this.comparer == null)
            {
                this.comparer = new WeakKeyComparer <TKey>(comparer);
            }

            this.dictionary           = new Dictionary <object, TValue>(this.comparer);
            this.intervalForRefresh   = refreshInterval;
            this.countLimitForRefresh = refreshInterval;
        }
コード例 #3
0
        public WeakKeyReference(T target, WeakKeyComparer <T> comparer)
            : base(target, false)
        {
            if (target == null)
            {
                throw new ArgumentNullException(nameof(target));
            }

            // retain the object's hash code immediately so that even
            // if the target is GC'ed we will be able to find and
            // remove the dead weak reference.
            this.HashCode = comparer.GetHashCode(target);
        }
コード例 #4
0
        /// <summary>
        ///A test for GetHashCode
        ///</summary>
        void GetHashCodeTestHelper <E>(E e)
            where E : class
        {
            WeakKeyComparer <E> target = new WeakKeyComparer <E>()
            {
                _equalityComparer = EqualityComparer <E> .Default
            };
            var key = new WeakKey <E>()
            {
                _elementReference = new WeakReference(e)
            };

            int hash1 = target.GetHashCode(ref key, true);

            key.SetValue(e, false);

            int hash2 = target.GetHashCode(ref key, false);

            Assert.AreEqual(hash1, hash2);
        }
コード例 #5
0
 public WeakDictionary(int capacity, IEqualityComparer <TKey> comparer)
 {
     this.comparer = new WeakKeyComparer <TKey>(comparer);
     dictionary    = new Dictionary <object, TValue>(capacity, this.comparer);
 }
コード例 #6
0
        //
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        /// <summary>
        ///A test for Equals
        ///</summary>
        void EqualsTestHelper <E>(E e1, E e2)
            where E : class
        {
            WeakKeyComparer <E> target = new WeakKeyComparer <E>()
            {
                _equalityComparer = EqualityComparer <E> .Default
            };
            var key1 = new WeakKey <E>()
            {
                _elementReference = new WeakReference(e1)
            };
            var key2 = new WeakKey <E>()
            {
                _elementReference = new WeakReference(e2)
            };

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, true));
            Assert.IsFalse(target.Equals(ref key2, true, ref key1, true));

            Assert.IsTrue(target.Equals(ref key1, true, ref key1, true));
            Assert.IsTrue(target.Equals(ref key2, true, ref key2, true));

            key2.SetValue(e1, true);

            Assert.IsTrue(target.Equals(ref key1, true, ref key2, true));
            Assert.IsTrue(target.Equals(ref key2, true, ref key1, true));

            ((WeakReference)key1._elementReference).Target = null;

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, true));
            Assert.IsFalse(target.Equals(ref key2, true, ref key1, true));

            ((WeakReference)key2._elementReference).Target = null;

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, true));
            Assert.IsFalse(target.Equals(ref key2, true, ref key1, true));

            Assert.IsTrue(target.Equals(ref key1, true, ref key1, true));
            Assert.IsTrue(target.Equals(ref key2, true, ref key2, true));

            key1.SetValue(e1, true);
            key2.SetValue(e2, false);

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, false));
            Assert.IsFalse(target.Equals(ref key2, false, ref key1, true));

            Assert.IsTrue(target.Equals(ref key1, true, ref key1, true));
            Assert.IsTrue(target.Equals(ref key2, false, ref key2, false));

            key2.SetValue(e1, false);

            Assert.IsTrue(target.Equals(ref key1, true, ref key2, false));
            Assert.IsTrue(target.Equals(ref key2, false, ref key1, true));

            key2.SetValue(null, false);

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, false));
            Assert.IsFalse(target.Equals(ref key2, false, ref key1, true));

            ((WeakReference)key1._elementReference).Target = null;

            Assert.IsFalse(target.Equals(ref key1, true, ref key2, false));
            Assert.IsFalse(target.Equals(ref key2, false, ref key1, true));

            key1.SetValue(e1, false);
            key2.SetValue(e2, false);

            Assert.IsFalse(target.Equals(ref key1, false, ref key2, false));
            Assert.IsFalse(target.Equals(ref key2, false, ref key1, false));

            key2.SetValue(e1, false);

            Assert.IsTrue(target.Equals(ref key1, false, ref key2, false));
            Assert.IsTrue(target.Equals(ref key2, false, ref key1, false));
        }