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

B

bonk

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.
 
N

Nicholas Paldino [.NET/C# MVP]

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

typeof(AnotherType).IsAssignableFrom(myTypeInstance);

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.
 
G

Guest

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 said:
The easiest way (although not the most correct way to do this) would be
to call IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInstance);

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]
- (e-mail address removed)

bonk said:
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.
 
N

Nicholas Paldino [.NET/C# MVP]

David,

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

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

David Anton said:
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 said:
The easiest way (although not the most correct way to do this) would
be
to call IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInstance);

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]
- (e-mail address removed)

bonk said:
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.
 
M

Mythran

Nicholas Paldino said:
David,

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

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

David Anton said:
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 said:
The easiest way (although not the most correct way to do this) would
be
to call IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInstance);

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]
- (e-mail address removed)

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
 
G

Guest

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 said:
David,

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

--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

David Anton said:
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 said:
The easiest way (although not the most correct way to do this) would
be
to call IsAssignableFrom, like so:

typeof(AnotherType).IsAssignableFrom(myTypeInstance);

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]
- (e-mail address removed)

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.
 
B

bonk

David said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top