Recently I got the desire to look at the types in a library assembly from an inheritance point of view, but I couldn't find a tool that did so. ( If you know of what that can, let me know! ( Reflector comes close, with the SubTypes tree under each type, but it only lets me see one at a time ) ) If you don't know what I mean... Imagine a tree view of the types in an assembly, but instead of being nested by namespace, they are nested by inheritance, ignoring namespace. So if you were to see this treeview applied to the whole .net framework, you'd see System.Object at the very top, with a whole whack of types below that, with their own subclasses beneath them.
Anyway, I figured it wouldn't be hard to do this myself, and it wasn't. Here's the worker component for a mickey mouse app I wrote that lets me point at an assembly and save the report as an xml file. I'm no xml ninja, so my code might not be the best, but hey, it works for me in the blink of an eye. It would be trivial to make an app that used this to bind it to a TreeView.
using System;
namespace ClassInheritanceView {
public class AssemblyViewGenerator : System.ComponentModel.Component {
public System.Reflection.Assembly TargetAssembly {
get { return this.targetAssembly; }
set { this.targetAssembly = value; }
}
public System.Xml.XmlDocument GenerateReport() {
this.reportDocument = new System.Xml.XmlDocument();
this.reportDocument.LoadXml(@"
]>
");
this.LoadTypes();
this.OrderTypes();
return reportDocument;
}
private void LoadTypes() {
foreach( Type type in this.targetAssembly.GetTypes() ) {
System.Xml.XmlNode typeNode = this.CreateNodeFromType(type);
this.reportDocument.DocumentElement.AppendChild(typeNode);
}
}
private System.Xml.XmlNode CreateNodeFromType( System.Type type ) {
String baseTypeValue;
if ( type.BaseType != null ) {
baseTypeValue = type.BaseType.FullName;
} else {
baseTypeValue = "Interface";
}
System.Xml.XmlElement typeNode = this.reportDocument.CreateElement("Type");
typeNode.SetAttribute("FullName", type.FullName);
typeNode.SetAttribute("BaseType",baseTypeValue);
return typeNode;
}
private void OrderTypes() {
System.Xml.XmlNodeList types = this.reportDocument.DocumentElement.ChildNodes;
Int32 childNodeCount = types.Count;
for( Int32 i = childNodeCount - 1; i >= 0; i-- ) {
System.Xml.XmlNode typeNode = types[i];
String parent = typeNode.Attributes.GetNamedItem("BaseType").Value;
System.Xml.XmlNode baseTypeNode = this.reportDocument.GetElementById(parent);
if ( baseTypeNode != null ) {
baseTypeNode.AppendChild(typeNode);
}
}
}
private System.Reflection.Assembly targetAssembly;
private System.Xml.XmlDocument reportDocument;
}
}
P.S.
The example I gave of seeing "Object" at the top won't actually work, because for some reason I can't actually load the mscorlib assembly. If anybody knows why, let me know.