Inheritance question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • hello_world

    Inheritance question

    Let's say I have the following class hierarchy:

    A: B
    B: C

    I would like a means to test whether class C is a 'descendant' of class
    A (or A is the 'ancestor' of C). If c is the instance of C,
    c.GetType().Bas eType only gives me type that is directly inherited, and
    i need to get the type(s) beyond that. Thanks!

  • Jon Skeet [C# MVP]

    #2
    Re: Inheritance question

    hello_world wrote:[color=blue]
    > Let's say I have the following class hierarchy:
    >
    > A: B
    > B: C
    >
    > I would like a means to test whether class C is a 'descendant' of class
    > A (or A is the 'ancestor' of C). If c is the instance of C,
    > c.GetType().Bas eType only gives me type that is directly inherited, and
    > i need to get the type(s) beyond that. Thanks![/color]

    Well, you can use BaseType on the the type returned by
    GetType().BaseT ype of course:

    Type x = c.GetType().Bas eType.BaseType

    You might want to look at the "is" and "as" operators though for most
    of the problems you'll run across when you want to know this kind of
    thing.

    Jon

    Comment

    • Yury.Korolev@gmail.com

      #3
      Re: Inheritance question

      Use IsSubclass method:

      c.GetType().IsS ubclassOf(typeo f(A))

      Comment

      • hello_world

        #4
        Re: Inheritance question

        Thanks for your input; that's exactly what i wanted!

        Comment

        • hello_world

          #5
          Re: Inheritance question

          In fact my example is wrong, as you might have guessed :P It should
          have been:

          B: A
          C: B

          Thanks again, i'll use the IsSubclass method - it will surely do the
          trick!

          Comment

          • Jon Skeet [C# MVP]

            #6
            Re: Inheritance question

            hello_world wrote:[color=blue]
            > In fact my example is wrong, as you might have guessed :P It should
            > have been:
            >
            > B: A
            > C: B
            >
            > Thanks again, i'll use the IsSubclass method - it will surely do the
            > trick![/color]

            Unless you've actually got the *types*, using "is" or "as" will be a
            lot faster, and more obvious in terms of meaning too:

            if (c is A)

            or

            A x = c as A;
            if (x != null)
            {
            // Work with x here
            }

            Jon

            Comment

            • hello_world

              #7
              Re: Inheritance question

              Thanks Jon, i'll give it a try - it seems less expensive in terms of
              calls to methods.

              Comment

              • Ryan McFarren

                #8
                Re: Inheritance question


                You can just use "is".

                if (c is A)

                Or, if you really want, you can keep digging down the descendent chain:

                c.GetType().Bas eType.BaseType. ..
                ---
                Posted via www.DotNetSlackers.com

                Comment

                Working...