By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,203 Members | 1,549 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,203 IT Pros & Developers. It's quick & easy.

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

P: n/a
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(AnotherType)

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

myTypeInstance.BaseType == typeof(AnotherType)

does the same, only for the direct base class.

Sep 19 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
Hi,

Di dyou try Type.IsSubClassOf ()

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegrou ps.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(AnotherType)

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

myTypeInstance.BaseType == typeof(AnotherType)

does the same, only for the direct base class.

Sep 19 '06 #2

P: n/a
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(AnotherType)

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

myTypeInstance.BaseType == typeof(AnotherType)

does the same, only for the direct base class.

Sep 19 '06 #3

P: n/a
The easiest way (although not the most correct way to do this) would be
to call IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInstanc e);

If AnotherType derives from myTypeInstance, then this will return true.
However, IsAssignableFrom 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.BaseType;
}

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

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

"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegrou ps.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(AnotherType)

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

myTypeInstance.BaseType == typeof(AnotherType)

does the same, only for the direct base class.

Sep 19 '06 #4

P: n/a
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 IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInstanc e);

If AnotherType derives from myTypeInstance, then this will return true.
However, IsAssignableFrom 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.BaseType;
}

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

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

"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegrou ps.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(AnotherType)

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

myTypeInstance.BaseType == typeof(AnotherType)

does the same, only for the direct base class.


Sep 19 '06 #5

P: n/a
David,

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

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

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:87**********************************@microsof t.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 IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInstan ce);

If AnotherType derives from myTypeInstance, then this will return
true.
However, IsAssignableFrom 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.BaseType;
}

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

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

"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegro ups.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(AnotherType)

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

myTypeInstance.BaseType == typeof(AnotherType)

does the same, only for the direct base class.



Sep 19 '06 #6

P: n/a

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

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

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

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:87**********************************@microsof t.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 IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInsta nce);

If AnotherType derives from myTypeInstance, then this will return
true.
However, IsAssignableFrom 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.BaseType;
}

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

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

"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegr oups.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(AnotherType)

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

myTypeInstance.BaseType == typeof(AnotherType)

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

P: n/a
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.com

"David Anton" <Da********@discussions.microsoft.comwrote in message
news:87**********************************@microsof t.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 IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInstanc e);

If AnotherType derives from myTypeInstance, then this will return
true.
However, IsAssignableFrom 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.BaseType;
}

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

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

"bonk" <sc******************@gmx.dewrote in message
news:11*********************@m7g2000cwm.googlegrou ps.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(AnotherType)

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

myTypeInstance.BaseType == typeof(AnotherType)

does the same, only for the direct base class.



Sep 19 '06 #8

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.