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.LoadFi le(openFileDial og.FileName);
foreach (Type assemblyType in assembly.GetTyp es())
{
Type type = assemblyType.Ge tType();
while (type != null && type.GetType() != typeof(DataSet) )
{
if (type.IsInstanc eOfType(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, WindowsApplicat ion1.Form1. Unfortunately,
invocations of .GetType() and .GetBaseType() yield the following
progression: System.RunTimeT ype, System.Type, System.Reflecti on.MemberInfo,
and finally System.Object (not System.Windows. Forms.Form,
System.Windows. Forms.Container Control, 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.IsInstance OfType()? 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 2790
> Type type = assemblyType.Ge tType();
assemblyType is already a Type instance. Type.GetType() is, effectively, the Type of Type. :) if (type.IsInstanc eOfType(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.ValueTyp e 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 ).IsAssignableF rom(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.LoadFi le(openFileDial og.FileName);
foreach (Type type in assembly.GetTyp es())
{
if (type == null)
{
// You won't enter this block, since GetTypes() will not contain a *null* Type.
}
if (typeof(DataSet ).IsAssignableF rom(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.IsAssigna bleFrom(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.IsInstance OfType()? 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".
IsInstanceOfTyp e() 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..jwaon line..com
-----------------------------------------------------------------------
"Zachary Hartnett" <No****@NoThank s.No> wrote in message news:ul******** ******@tk2msftn gp13.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.LoadFi le(openFileDial og.FileName);
foreach (Type assemblyType in assembly.GetTyp es()) { Type type = assemblyType.Ge tType();
while (type != null && type.GetType() != typeof(DataSet) ) { if (type.IsInstanc eOfType(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, WindowsApplicat ion1.Form1. Unfortunately, invocations of .GetType() and .GetBaseType() yield the following progression: System.RunTimeT ype, System.Type, System.Reflecti on.MemberInfo, and finally System.Object (not System.Windows. Forms.Form, System.Windows. Forms.Container Control, 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.IsInstance OfType()? 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 symbols loaded. I use VS
v1.7.3088 and .NET framework v1.1.4322 SP1.
I work with ASP.NET using VB and I'm trying to set a breakpoint on the
ascx.vb file.
|
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 examples?
|
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 later ported to java and Python for comparison.
http://bplusdotnet.sourceforge.net/ for full details and code
]
|
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 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:
------------------------------------------------------------...
| |
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 "Init" sequence I load my template and then load the body content
of the page which is pasted into the template.
However, if I post back I get an error of...
"Failed to load viewstate. The control tree into which viewstate is being
|
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 looks the data up in the
tree.
|
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.
If you are interested in how builtin/pure-python inheritance interacts
|
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 Shared Function InjectFieldValues(ByVal p_def As T,
ByVal p_properties As PropertyMaps) As T
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |