473,224 Members | 1,483 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

Walking Inheritance Tree of Loaded Assembly

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!
Nov 17 '05 #1
1 2767
> 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 ;)
--
Dave Sexton
dave@www..jwaonline..com
-----------------------------------------------------------------------
"Zachary Hartnett" <No****@NoThanks.No> wrote in message news:ul**************@tk2msftngp13.phx.gbl...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!

Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: asdf | last post by:
Hello, I was enjoying working in VS for half a year without any problems and now I cannot debug anymore. Without any really reason my Studio tells me that the page that I want to debug has - No...
5
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
22
by: Matthew Louden | last post by:
I want to know why C# doesnt support multiple inheritance? But why we can inherit multiple interfaces instead? I know this is the rule, but I dont understand why. Can anyone give me some concrete...
3
by: Aaron Watters | last post by:
A C# question about constructors/static methods and inheritance: Please help me make my code simpler! For fun and as an exercise I wrote somewhat classical B-tree implementation in C# which I...
0
by: Zachary Hartnett | last post by:
This might be a double post... I'm not sure why the forum looked like it deleted this message... I was trying to write a routine this morning that would open a given assembly, walk the...
0
by: Alistair McRonald | last post by:
I have been experimenting with an ASP.Net templating system based around inheriting from the System.Web.UI.Page class. I am however having some issues with loading the viewstate. During the...
4
by: Ken | last post by:
I have a binary tree in VB NET and insertions seem to be slow. The program receives data from one source and inserts it into the tree. The program receives data from another source and...
0
by: nejucomo | last post by:
Hi folks, Quick Synopsis: A test script demonstrates a memory leak when I use pythonic extensions of my builtin types, but if I use the builtin types themselves there is no memory leak. ...
11
by: Simon Woods | last post by:
Hi I have this recursive function and I want to walk the inheritance hierarchy to set field values .... the generic T is constrainted as the base class of the inheritance hierarchy Friend...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.