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
{
public class TypeInheritance
{
#region Fields
private Type type;
private List<Type> chainUpToThis = null;
#endregion
#region Constructors
public TypeInheritance(Type t)
{
type = t;
}
public TypeInheritance(object o)
{
type = o.GetType();
}
#endregion
#region Overrides
public override string ToString()
{
EnsureChainUpToAvailable();
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
public Type[] ToArray()
{
return EnsureChainUpToAvailable().ToArray();
}
#endregion
#region Private Methods
private List<Type> EnsureChainUpToAvailable()
{
if (chainUpToThis == null)
{
chainUpToThis = new List<Type>();
while (type != null)
{
chainUpToThis.Insert(0, type);
type = type.BaseType;
}
}
return chainUpToThis;
}
#endregion
}
}
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?