Click here to Skip to main content
Click here to Skip to main content

Tagged as

Following object inheritance

, 23 Jul 2010 CPOL
Rate this:
Please Sign up or sign in to vote.
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)

Share

About the Author

OriginalGriff
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?
Follow on   Google+

Comments and Discussions

 
GeneralReason for my vote of 2 Good job and an interesting idea, bu... PinmemberKubajzz30-Jul-10 3:04 

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

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

| Advertise | Privacy | Mobile
Web01 | 2.8.141022.2 | Last Updated 23 Jul 2010
Article Copyright 2010 by OriginalGriff
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid