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!
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!