Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

Following object inheritance

4.29/5 (7 votes)
23 Jul 2010CPOL 14.1K  
Sometimes, it helps to know the complete inheritance of an object or type in order to understand what can be done with it. This Tip presents a simple C# class to access the information
Recently, when answering a Q&A about TreeView and TreeNodes, I wanted to be certain that what I was saying was absolutely correct - TreeNode does not derive from Control, and so cannot be displayed.
But finding this inheritance was not simple, so I wrote a quick class to get such information. Since it may be of use to others, I present it here:
using System;
using System.Collections.Generic;
using System.Text;

namespace UtilityControls
    {
    /// <summary>
    /// Provides information on class inheritance
    /// </summary>
    public class TypeInheritance
        {
        #region Fields
        /// <summary>
        /// Internal, type of object to report
        /// </summary>
        private Type type;
        /// <summary>
        /// Internal, list of type inherited from (starting with Object)
        /// </summary>
        private List<Type> chainUpToThis = null;
        #endregion

        #region Constructors
        /// <summary>
        /// Construct a TypeInheritance object from a type
        /// </summary>
        /// <param name="t">Type to report inheritance for.</param>
        public TypeInheritance(Type t)
            {
            type = t;
            }
        /// <summary>
        /// Construct a TypeInheritance object from an object instance
        /// </summary>
        /// <param name="o">Object to report inheritance for.</param>
        public TypeInheritance(object o)
            {
            type = o.GetType();
            }
        #endregion

        #region Overrides
        /// <summary>
        /// Convert a TypeInheritance to a string.
        /// Reports the inheritance that lead to this object type,
        /// in the form "Object->..."
        /// </summary>
        /// <returns></returns>
        public override string ToString()
            {
            EnsureChainUpToAvailable();
            // Convert the chain of objects that lead up to this to a string.
            StringBuilder sb = new StringBuilder(128);
            string prefix = "";
            foreach (Type t in chainUpToThis)
                {
                sb.Append(prefix + t.Name);
                prefix = "->";
                }
            return sb.ToString();
            }
        #endregion

        #region Public Methods
        /// <summary>
        /// Return the inheritance chain as a array, starting with Object
        /// </summary>
        /// <returns>The inheritance chain as a array, starting with Object</returns>
        public Type[] ToArray()
            {
            return EnsureChainUpToAvailable().ToArray();
            }
        #endregion

        #region Private Methods
        /// <summary>
        /// Ensures the chainUpToThis list has been filled in, by creating it if it hasn't
        /// </summary>
        private List<Type> EnsureChainUpToAvailable()
            {
            if (chainUpToThis == null)
                {
                // Not done yet - create it.
                chainUpToThis = new List<Type>();
                while (type != null)
                    {
                    chainUpToThis.Insert(0, type);
                    type = type.BaseType;
                    }
                }
            return chainUpToThis;
            }
        #endregion
        }
    }

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)