473,386 Members | 1,699 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,386 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 2774
> 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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

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.