473,756 Members | 2,900 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

find out if an instance of type type (indirectly) derives from another type

When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?

myTypeInstance == typeof(AnotherT ype)

only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFro mAnotherType" the above evaluates to false..

myTypeInstance. BaseType == typeof(AnotherT ype)

does the same, only for the direct base class.

Sep 19 '06 #1
8 2379
Hi,

Di dyou try Type.IsSubClass Of ()

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"bonk" <sc************ ******@gmx.dewr ote in message
news:11******** *************@m 7g2000cwm.googl egroups.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?

myTypeInstance == typeof(AnotherT ype)

only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFro mAnotherType" the above evaluates to false..

myTypeInstance. BaseType == typeof(AnotherT ype)

does the same, only for the direct base class.

Sep 19 '06 #2
Try:
myTypeInstance is AnotherType
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"bonk" wrote:
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?

myTypeInstance == typeof(AnotherT ype)

only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFro mAnotherType" the above evaluates to false..

myTypeInstance. BaseType == typeof(AnotherT ype)

does the same, only for the direct base class.

Sep 19 '06 #3
The easiest way (although not the most correct way to do this) would be
to call IsAssignableFro m, like so:

typeof(AnotherT ype).IsAssignab leFrom(myTypeIn stance);

If AnotherType derives from myTypeInstance, then this will return true.
However, IsAssignableFro m does a lot more than just check the inheritance
chain, it checks interfaces, generic types, etc, etc.

A better solution is this:

static bool TypeDerivesFrom (Type base, Type derived)
{
// Removed checking against null for clarity.

// An easy assumption, not necessarily needed.
if (base == typeof(object))
{
// Return true.
return true;
}

// Compare while true.
do while (derived != null)
{
// If the base class is equal to the derived class, then
// return true.
if (base == derived)
{
// Get out.
return true;
}

// Set the derived class equal it's base type.
derived = derived.BaseTyp e;
}

// The type does not derive from another type.
return false;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"bonk" <sc************ ******@gmx.dewr ote in message
news:11******** *************@m 7g2000cwm.googl egroups.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?

myTypeInstance == typeof(AnotherT ype)

only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFro mAnotherType" the above evaluates to false..

myTypeInstance. BaseType == typeof(AnotherT ype)

does the same, only for the direct base class.

Sep 19 '06 #4
Wow - are you sure you're not just rewriting the "is" operator?
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
The easiest way (although not the most correct way to do this) would be
to call IsAssignableFro m, like so:

typeof(AnotherT ype).IsAssignab leFrom(myTypeIn stance);

If AnotherType derives from myTypeInstance, then this will return true.
However, IsAssignableFro m does a lot more than just check the inheritance
chain, it checks interfaces, generic types, etc, etc.

A better solution is this:

static bool TypeDerivesFrom (Type base, Type derived)
{
// Removed checking against null for clarity.

// An easy assumption, not necessarily needed.
if (base == typeof(object))
{
// Return true.
return true;
}

// Compare while true.
do while (derived != null)
{
// If the base class is equal to the derived class, then
// return true.
if (base == derived)
{
// Get out.
return true;
}

// Set the derived class equal it's base type.
derived = derived.BaseTyp e;
}

// The type does not derive from another type.
return false;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"bonk" <sc************ ******@gmx.dewr ote in message
news:11******** *************@m 7g2000cwm.googl egroups.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?

myTypeInstance == typeof(AnotherT ype)

only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFro mAnotherType" the above evaluates to false..

myTypeInstance. BaseType == typeof(AnotherT ype)

does the same, only for the direct base class.


Sep 19 '06 #5
David,

Yeah, I realized that was exactly what I was doing.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:87******** *************** ***********@mic rosoft.com...
Wow - are you sure you're not just rewriting the "is" operator?
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
> The easiest way (although not the most correct way to do this) would
be
to call IsAssignableFro m, like so:

typeof(Another Type).IsAssigna bleFrom(myTypeI nstance);

If AnotherType derives from myTypeInstance, then this will return
true.
However, IsAssignableFro m does a lot more than just check the inheritance
chain, it checks interfaces, generic types, etc, etc.

A better solution is this:

static bool TypeDerivesFrom (Type base, Type derived)
{
// Removed checking against null for clarity.

// An easy assumption, not necessarily needed.
if (base == typeof(object))
{
// Return true.
return true;
}

// Compare while true.
do while (derived != null)
{
// If the base class is equal to the derived class, then
// return true.
if (base == derived)
{
// Get out.
return true;
}

// Set the derived class equal it's base type.
derived = derived.BaseTyp e;
}

// The type does not derive from another type.
return false;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"bonk" <sc************ ******@gmx.dewr ote in message
news:11******* **************@ m7g2000cwm.goog legroups.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?

myTypeInstance == typeof(AnotherT ype)

only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFro mAnotherType" the above evaluates to false..

myTypeInstance. BaseType == typeof(AnotherT ype)

does the same, only for the direct base class.



Sep 19 '06 #6

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c omwrote in
message news:%2******** ********@TK2MSF TNGP03.phx.gbl. ..
David,

Yeah, I realized that was exactly what I was doing.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:87******** *************** ***********@mic rosoft.com...
>Wow - are you sure you're not just rewriting the "is" operator?
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
>> The easiest way (although not the most correct way to do this) would
be
to call IsAssignableFro m, like so:

typeof(Anothe rType).IsAssign ableFrom(myType Instance);

If AnotherType derives from myTypeInstance, then this will return
true.
However, IsAssignableFro m does a lot more than just check the
inheritance
chain, it checks interfaces, generic types, etc, etc.

A better solution is this:

static bool TypeDerivesFrom (Type base, Type derived)
{
// Removed checking against null for clarity.

// An easy assumption, not necessarily needed.
if (base == typeof(object))
{
// Return true.
return true;
}

// Compare while true.
do while (derived != null)
{
// If the base class is equal to the derived class, then
// return true.
if (base == derived)
{
// Get out.
return true;
}

// Set the derived class equal it's base type.
derived = derived.BaseTyp e;
}

// The type does not derive from another type.
return false;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"bonk" <sc************ ******@gmx.dewr ote in message
news:11****** *************** @m7g2000cwm.goo glegroups.com.. .
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?

myTypeInstan ce == typeof(AnotherT ype)

only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFr omAnotherType" the above evaluates to false..

myTypeInstance .BaseType == typeof(AnotherT ype)

does the same, only for the direct base class.


lol exactly what I was doing on another thread :P I was using reflection
instead of the "is" operator heh.

must be a virus floating around affecting us...

Mythran
Sep 19 '06 #7
I'm a lazy programmer - sometimes that pays off, sometimes it doesn't ;)
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
David,

Yeah, I realized that was exactly what I was doing.

--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"David Anton" <Da********@dis cussions.micros oft.comwrote in message
news:87******** *************** ***********@mic rosoft.com...
Wow - are you sure you're not just rewriting the "is" operator?
--
David Anton
www.tangiblesoftwaresolutions.com
Instant C#: VB to C# converter
Instant VB: C# to VB converter
Instant C++: C#/VB to C++ converter
C# Code Metrics: Quick metrics for C#
"Nicholas Paldino [.NET/C# MVP]" wrote:
The easiest way (although not the most correct way to do this) would
be
to call IsAssignableFro m, like so:

typeof(AnotherT ype).IsAssignab leFrom(myTypeIn stance);

If AnotherType derives from myTypeInstance, then this will return
true.
However, IsAssignableFro m does a lot more than just check the inheritance
chain, it checks interfaces, generic types, etc, etc.

A better solution is this:

static bool TypeDerivesFrom (Type base, Type derived)
{
// Removed checking against null for clarity.

// An easy assumption, not necessarily needed.
if (base == typeof(object))
{
// Return true.
return true;
}

// Compare while true.
do while (derived != null)
{
// If the base class is equal to the derived class, then
// return true.
if (base == derived)
{
// Get out.
return true;
}

// Set the derived class equal it's base type.
derived = derived.BaseTyp e;
}

// The type does not derive from another type.
return false;
}

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"bonk" <sc************ ******@gmx.dewr ote in message
news:11******** *************@m 7g2000cwm.googl egroups.com...
When I have an instance of an object wich is of type System.Type, how
can I find out if it directly or indirecly derives from another type?

myTypeInstance == typeof(AnotherT ype)

only seems to check if they are ultimately the same type (at the very
bottom of the inheritance hirarchy) but in case myTypeInstance is
"TypeDerivedFro mAnotherType" the above evaluates to false..

myTypeInstance. BaseType == typeof(AnotherT ype)

does the same, only for the direct base class.



Sep 19 '06 #8

David Anton schrieb:
Try:
myTypeInstance is AnotherType
--
This will always evaluate to false since myTypeInstance is always of
type System.Type (see my original post) and AnotherType is not.

Sep 20 '06 #9

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

Similar topics

1
2959
by: Amir | last post by:
Hi all, I have a table called PTRANS with few columns (see create script below). I have created a view on top that this table VwTransaction (See below) I can now run this query without a problem: select * from dbo.VwTransaction where
2
58424
by: andrew | last post by:
C:\Documents and Settings\watts\My Documents\Visual Studio Projects\boeing\showPMACfg\vuePMAcfg\vuePMAcfg.cs(88): Static member 'vuePMAcfg.pmaDataHash.pmaDataHash1' cannot be accessed with an instance reference; qualify it with a type name instead OK I have this class And the hash and the function shows up in type ahead. I tried declaring it and later accessing it like this:
2
7146
by: Daniel | last post by:
Hello, This is maybe a simple one: Is there any way to check, if a string can be converted to another type (like DateTime or Integer) without using try/catch? In VB, i can use IsDate or IsNumeric to do this, but how can i do it in C#? Thanks in advance, Daniel
2
2466
by: Daniel | last post by:
Hello, maybe this is an easy one: Is there any way to check, if a string value can be converted into another type (e.g. DateTime, Integer) without using exceptions (try/catch)? Thanks in advance, Daniel
4
27939
by: m96 | last post by:
hi, i'm trying to make a query to a ldap server (version v2 or v3 doen't matter) with c#. the query works just fine but the problem is that i can't read the custom attributes/fields, since .net gives the following error: System.NotImplementedException: Handling of this ADSVALUE type is not yet implemented (type = 0xb). after googling for a long time i found out that many other have the same
16
11281
by: Martin Jørgensen | last post by:
Hi, Short question: Any particular reason for why I'm getting a warning here: (cast from function call of type int to non-matching type double) xdouble = (double)rand()/(double)RAND_MAX;
7
1751
by: tadmill | last post by:
Is it possible for a class that accepts a generic type, to re-create another instance of itself with a property type of the passed class? ex. public class SomeClass<T> { private PropertyInfo propInfos; private Type objectType = typeof(T); public SomeClass()
7
6536
by: DamienS | last post by:
Hi, I need to determine if one type is inheritted from another type. I don't have instances of the objects (and it's undesirable to create them). Can someone please point me in the right direction? Thanks in advance,
2
4824
by: alireza6485 | last post by:
Hi: I have 3 class/Methods: the first one:just beeps the second one:ask the user to Enter the number of beep The last one:Activate and runs. I get the following Error:"instance reference; qualify it with a type name instead " Can you please re-write the code and fix it for me,Thanks. First One:
0
9456
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, 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...
0
9275
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,...
0
9872
jinu1996
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...
1
9843
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,...
0
8713
agi2029
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...
0
6534
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5142
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...
0
5304
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3805
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 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.