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! 1 2770
> 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! This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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
/ \ / \ / \
|
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...
|
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...
|
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...
|
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...
|
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...
|
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.
...
|
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...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
| |