Click here to Skip to main content
15,881,687 members
Articles / Programming Languages / C#
Tip/Trick

Following object inheritance

Rate me:
Please Sign up or sign in to vote.
4.29/5 (7 votes)
23 Jul 2010CPOL 13.5K   5   2
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)


Written By
CEO
Wales Wales
Born at an early age, he grew older. At the same time, his hair grew longer, and was tied up behind his head.
Has problems spelling the word "the".
Invented the portable cat-flap.
Currently, has not died yet. Or has he?

Comments and Discussions

 
Questioncould you share an example how to use this tool? Pin
Southmountain28-Dec-19 8:04
Southmountain28-Dec-19 8:04 
GeneralReason for my vote of 2 Good job and an interesting idea, bu... Pin
Kubajzz30-Jul-10 3:04
Kubajzz30-Jul-10 3:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.