Click here to Skip to main content
15,895,423 members
Articles / Programming Languages / C# 4.0

Universal Type Extender

Rate me:
Please Sign up or sign in to vote.
4.83/5 (30 votes)
5 Jun 2013Ms-PL11 min read 73.6K   569   62  
Emulate extension properties by extending any reference type with any other type.
// file     : ExtensionRepository.RepositoryItemTargetKey.cs
// project  : UniversalTypeExtender
// author   : Thorsten Bruning
// date     : 18.10.2011

using System;

namespace TB.ComponentModel.Infrastructure {
    partial class ExtensionRepository {

        /// <summary>
        /// Represents the key of an ExtensionRepositoryItem.
        /// Only for infrastructure.
        /// </summary>
        public class RepositoryItemTargetKey {

            private readonly WeakReference mTarget;
            private readonly int mHashCode;

            /// <summary>
            /// Gets the target.
            /// </summary>
            public object Target {
                get { return mTarget.Target; }
            }

            /// <summary>
            /// Initializes a new instance of the RepositoryItemTargetKey class.
            /// </summary>
            /// <param name="target">RepositoryItem.</param>
            public RepositoryItemTargetKey(object target) {
                mTarget = new WeakReference(target);
                if(target != null) {
                    mHashCode = target.GetHashCode();
                }
            }

            /// <summary>
            /// Serves as a hash function for this type.
            /// </summary>
            /// <returns>A hash code for this instance.</returns>
            public override int GetHashCode() {
                return mHashCode;
            }

            /// <summary>
            /// Determines whether the specified object is equal to this instance.
            /// </summary>
            /// <param name="obj">The object to compare with this instance.</param>
            /// <returns>true if the specified object is equal to this instance otherwise, false.</returns>
            public override bool Equals(object obj) {
                if (obj == null) {
                    return false;
                }
                if (obj.GetType() != GetType()) {
                    return false;
                }
                object target = Target;
                object objectsTarget = ((RepositoryItemTargetKey)obj).Target;
                if (target == null && objectsTarget == null) {
                    return true;
                }
                if (target == null) {
                    return false;
                }
                if(objectsTarget == null) {
                    return false;
                }
                return target.Equals(objectsTarget);
            }

        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


Written By
Software Developer
Germany Germany
--------------------------------
Thank you for voting
Best "Everything Else" Article
of September 2014

Comments and Discussions