Weak reference that can be used in collections. It is equal to the object it references and has the same hash code.
Inheritance: System.WeakReference
        public void ReferencesIsEqualToTheInstanceItIsReferringTo()
        {
            var instance = new object();
            var reference = new ReferenceEqualWeakReference(instance);

            reference.Equals(instance).Should().BeTrue();
            reference.GetHashCode().Should().Be(instance.GetHashCode());
        }
        public void TwoReferencesReferencingTheSameObjectAreEqual()
        {
            var instance = new object();
            var ref1 = new ReferenceEqualWeakReference(instance);
            var ref2 = new ReferenceEqualWeakReference(instance);

            ref1.Equals(ref2).Should().BeTrue();
            ref1.GetHashCode().Should().Be(ref2.GetHashCode());
        }
Example #3
0
        /// <summary>
        /// Stores the specified context in the cache.
        /// </summary>
        /// <param name="context">The context to store.</param>
        /// <param name="reference">The instance reference.</param>
        public void Remember(IContext context, InstanceReference reference)
        {
            var scope = context.GetScope();
            var entry = new CacheEntry(context, reference);

            lock (this.entries)
            {
                var weakScopeReference = new ReferenceEqualWeakReference(scope);
                if (!this.entries.ContainsKey(weakScopeReference))
                {
                    this.entries[weakScopeReference] = new Multimap<IBindingConfiguration, CacheEntry>();
                    var notifyScope = scope as INotifyWhenDisposed;
                    if (notifyScope != null)
                    {
                        notifyScope.Disposed += (o, e) => this.Clear(weakScopeReference);
                    }
                }

                this.entries[weakScopeReference].Add(context.Binding.BindingConfiguration, entry);
            }
        }