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

Tech-Archive recommends: Fix windows errors by optimizing your registry




"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


.



Relevant Pages

  • Re: XXXX_Load Event vs OnXXXX
    ... Overriding the OnXXXX does require you to write more code, ... >> the OnFoo method. ... >> method that overrides it's base class A' OnFoo method, ...
    (microsoft.public.dotnet.languages.vb)
  • Re: find out if an instance of type type (indirectly) derives from
    ... Instant VB: C# to VB converter ... C# Code Metrics: Quick metrics for C# ... If AnotherType derives from myTypeInstance, ... only for the direct base class. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: std::map crash
    ... > Often it is stated that one should not "ever" derive from the Standard ... while the absence of virtual functions in STL containers makes them ... possible to override a virtual function, even one declared in a base class ... like the OP's that derives from a template that isn't fully specialized, ...
    (microsoft.public.vc.stl)
  • RE: Structure of website
    ... Basically, I want to have a base class that on the first call to the page, ... What may add to the confusion is that my page, user controls and custom ... which can pass to the UC codebehind. ... derived class, which derives from usercontrol. ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: Design question - Abstract, base, interfaces, ack!
    ... You are allowed to have non-abstract methods in an abstract base class that can ... Sequence Diagram Editor - A quick and easy way to draw and edit sequence diagrams. ... > is that the factory will determine what type of Shape object to create. ... // Defined in each class that derives from Shape. ...
    (microsoft.public.dotnet.languages.csharp)