Determining if one type inherits from another type

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

    Determining if one type inherits from another type

    Hi,

    I need to determine if one type is inheritted from another type. I
    don't have instances of the objects (and it's undesirable to create
    them).

    Can someone please point me in the right direction?

    Thanks in advance,


    Damien
  • Marc Gravell

    #2
    Re: Determining if one type inherits from another type

    For classes, you can use:

    type1.IsSubclas sOf(type2)

    If you mean interfaces, this i slightly tricker - but IsAssignableFro m
    might do the job.

    Marc

    Comment

    • Pavel Minaev

      #3
      Re: Determining if one type inherits from another type

      On Aug 21, 12:53 pm, Marc Gravell <marc.grav...@g mail.comwrote:
      For classes, you can use:
      >
      type1.IsSubclas sOf(type2)
      >
      If you mean interfaces, this i slightly tricker - but IsAssignableFro m
      might do the job.
      Note that IsSubclassOf will return false if type1==type2 (which is
      expected, given its name, but may be surprising in a sense that this
      behavior is different from the "is" operator).

      The most generic way to do this, which will handle the case described
      above, as well as interfaces, is indeed Type.IsAssignab leFrom.

      Comment

      • Marc Gravell

        #4
        Re: Determining if one type inherits from another type

        The most generic way to do this, which will handle the case described
        above, as well as interfaces, is indeed Type.IsAssignab leFrom.
        Granted; of course, it depends a bit on what is *actually* intended by
        "if one type is inheritted from another type", and what the OP wants
        that to return for "Foo op Foo".

        Marc

        Comment

        • G.S.

          #5
          Re: Determining if one type inherits from another type

          On Aug 21, 7:45 am, Pavel Minaev <int...@gmail.c omwrote:
          On Aug 21, 12:53 pm, Marc Gravell <marc.grav...@g mail.comwrote:
          >
          For classes, you can use:
          >
          type1.IsSubclas sOf(type2)
          >
          If you mean interfaces, this i slightly tricker - but IsAssignableFro m
          might do the job.
          >
          Note that IsSubclassOf will return false if type1==type2 (which is
          expected, given its name, but may be surprising in a sense that this
          behavior is different from the "is" operator).
          >
          The most generic way to do this, which will handle the case described
          above, as well as interfaces, is indeed Type.IsAssignab leFrom.
          To piggyback on this thread and learn something - can't the same be
          accomplished by using "is"?

          Comment

          • Marc Gravell

            #6
            Re: Determining if one type inherits from another type

            "is" will only work for an instance (left-hand operand) and a type
            (right-hand operand), so it depends on the full scenario. In this case
            it sounds like there is no instance, and the type is known only at
            runtime, so "is" can't be used. Of course, my interpretation of the
            problem might be incorrect...

            Marc

            Comment

            • Pavel Minaev

              #7
              Re: Determining if one type inherits from another type

              On Aug 21, 5:49 pm, "G.S." <gstoy...@gmail .comwrote:
              To piggyback on this thread and learn something - can't the same be
              accomplished by using  "is"?
              It can, if you have a specific value, and know the type you want to
              check for in advance. What we were discussing is rather this
              situation:

              void Foo(Type type1, Type type2) {
              // Need to determine of type1 inherits from type2
              }

              Comment

              • DamienS

                #8
                Re: Determining if one type inherits from another type

                Thanks for that guys. Very helpful.

                For the record...
                In this case
                it sounds like there is no instance, and the type is known only at
                runtime, so "is" can't be used.
                .... that was correct in this case.

                Damien

                Comment

                Working...