Walking Inheritance Tree of Loaded Assembly

Z

Zachary Hartnett

I was trying to write a routine this morning that would open a given
assembly, walk the inheritance tree of classes in the assembly, and provide
a list of classes in the assembly that inherit from DataSet.

Here is a snippet from the routine I came up with:
------------------------------------------------------------
openFileDialog.ShowDialog();

Assembly assembly =
Assembly.LoadFile(openFileDialog.FileName);

foreach (Type assemblyType in assembly.GetTypes())
{
Type type = assemblyType.GetType();

while (type != null && type.GetType() != typeof(DataSet))
{
if (type.IsInstanceOfType(new DataSet()))
{
// NOTE: Always evaluates to true on System.Object...
// break;
}

if (type == typeof(DataSet))
{
// NOTE: This never evaluates to true.
// break;
}

type = type.BaseType;
}

if (type != null)
{
// Do something spectacular.
}
}
------------------------------------------------------------

Interestingly enough, assemblyType behaves very nicely and indicates that it
is an instance of, say, WindowsApplication1.Form1. Unfortunately,
invocations of .GetType() and .GetBaseType() yield the following
progression: System.RunTimeType, System.Type, System.Reflection.MemberInfo,
and finally System.Object (not System.Windows.Forms.Form,
System.Windows.Forms.ContainerControl, et. al.).

Questions:
1. Is there a way to walk the inheritance tree of classes contained within a
loaded assembly at runtime?
2. Is there a mechanism for determining if a class inherits from a given
type without walking the inheritance tree? I would have expected the is
operator to work - it didn't, but this probably is due to the fact that the
loaded inheritance tree was different than I expected.
3. Why would an Object be an instance of type DataSet according to an
invocation of type.IsInstanceOfType()? I could see DataSet being an instance
of type Object, but not the other way around.

I imagine all this stems from security - it will be a shame, however, if I
must link directly to code for this type of operation (i.e. recompile the
utility and recode it to work on a hard-coded instance or type declaration
of a given class). My intent was to find any DataSets in the given assembly,
allow the user to select one, and provide an inherited code snippet that
adds a few dynamic properties and methods that couldn't be placed in an
interface.

Thanks in advance for your thoughts and clarifications!
 
D

Dave

Type type = assemblyType.GetType();

assemblyType is already a Type instance. Type.GetType() is, effectively, the Type of Type. :)
if (type.IsInstanceOfType(new DataSet()))
{
// NOTE: Always evaluates to true on System.Object...
// break;
}

Because every class/struct in .NET implicitly inherits from System.Object. (struct from System.ValueType which inherits from
System.Object)
if (type == typeof(DataSet))
{
// NOTE: This never evaluates to true.
// break;
}

Because you have not declared the Type "DataSet" in your assembly. Instead, you have declared a class that inherits from type
"DataSet". So, you need to check the following:

if (typeof(DataSet).IsAssignableFrom(type)) { ... }
if (type != null)
{
// Do something spectacular.
}
Agreed.

1. Is there a way to walk the inheritance tree of classes contained within a loaded assembly at runtime?
Assembly assembly =
Assembly.LoadFile(openFileDialog.FileName);

foreach (Type type in assembly.GetTypes())
{
if (type == null)
{
// You won't enter this block, since GetTypes() will not contain a *null* Type.
}

if (typeof(DataSet).IsAssignableFrom(type))
{
// Found a strong-typed DataSet in the assembly!
}

if (type == typeof(MyClass))
{
// Found MyClass in *my* assembly... go figure!
}

type = type.BaseType;

if (type == typeof(MyClass))
{
// Found a type that directly inherits from MyClass!
}
}
2. Is there a mechanism for determining if a class inherits from a given type without walking the inheritance tree?

I mentioned it above, "Type.IsAssignableFrom(Type)"
I would have expected the is operator to work - it didn't, but this probably is due to the fact that the loaded inheritance tree
was different than I expected.

You can't use the C# "is" operator on Type instances, because the "is" operator takes a Type name as the second argument. i.e:

Type obj;
if (obj is DataSet) { ... } // nope. obj is of the type, "Type".

DataSet obj;
if (typeof(obj) is DataSet) { ... } // Yep.
3. Why would an Object be an instance of type DataSet according to an invocation of type.IsInstanceOfType()? I could see DataSet
being an instance of type Object, but not the other way around.

You are correct that Object will never be an instance of the Type, "DataSet".
IsInstanceOfType() does not check if the calling instance is of the specified instance; i.e., it's the other way around.
I imagine all this stems from security - it will be a shame, however, if I

Nope, just a few misguided blocks of code ;)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top