Re: find out if an instance of type type (indirectly) derives from
- From: "Mythran" <kip_potter@xxxxxxxxxxx>
- Date: Tue, 19 Sep 2006 09:02:05 -0700
"Nicholas Paldino [.NET/C# MVP]" <mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote in
message news:%23f$M$KA3GHA.1288@xxxxxxxxxxxxxxxxxxxxxxx
David,
Yeah, I realized that was exactly what I was doing.
--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"David Anton" <DavidAnton@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:87A4CAE8-8999-47A7-9A51-2108F1D219AC@xxxxxxxxxxxxxxxx
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(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]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx
"bonk" <schwertfischtrombose@xxxxxx> wrote in message
news:1158677744.945033.95850@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
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
.
- References:
- find out if an instance of type type (indirectly) derives from another type
- From: bonk
- Re: find out if an instance of type type (indirectly) derives from another type
- From: Nicholas Paldino [.NET/C# MVP]
- Re: find out if an instance of type type (indirectly) derives from
- From: David Anton
- Re: find out if an instance of type type (indirectly) derives from
- From: Nicholas Paldino [.NET/C# MVP]
- find out if an instance of type type (indirectly) derives from another type
- Prev by Date: Re: Check permissions on Folder
- Next by Date: Re: Building .tbl
- Previous by thread: Re: find out if an instance of type type (indirectly) derives from
- Next by thread: Re: find out if an instance of type type (indirectly) derives from
- Index(es):
Relevant Pages
|