Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. System.Type usage

System.Type usage

Scheduled Pinned Locked Moved C#
questiontutorial
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    S O S
    wrote on last edited by
    #1

    Every different class/struct/whatever returns a unique Type class with the GetType() function, right? Even when the object is cast into Object, it still returns the previous one, right? Now how do I see if that type is derived from some other thing? For example, if I have Object o and I need to see if it derives from ISomething interface. If System.Type is not for this and does soemthing completely different, what way could I do this?

    A J 2 Replies Last reply
    0
    • S S O S

      Every different class/struct/whatever returns a unique Type class with the GetType() function, right? Even when the object is cast into Object, it still returns the previous one, right? Now how do I see if that type is derived from some other thing? For example, if I have Object o and I need to see if it derives from ISomething interface. If System.Type is not for this and does soemthing completely different, what way could I do this?

      A Offline
      A Offline
      Andres Manggini
      wrote on last edited by
      #2

      To test if an object implements an interface or derives from a type: if( theObject is ISomething ) { } or ISomething something = theObject as ISomething; In the latter something will set to null if theObject doesn't support the interface Andres Manggini. Buenos Aires - Argentina.

      S 1 Reply Last reply
      0
      • A Andres Manggini

        To test if an object implements an interface or derives from a type: if( theObject is ISomething ) { } or ISomething something = theObject as ISomething; In the latter something will set to null if theObject doesn't support the interface Andres Manggini. Buenos Aires - Argentina.

        S Offline
        S Offline
        S O S
        wrote on last edited by
        #3

        Ah, thank you. But how would I take the y in x is y from a variable? I mean a type that the variable stores. Something like this: ??? aaa = ???System.Exception // Checking against System.Exception for example if (blah is ???aaa)

        1 Reply Last reply
        0
        • S S O S

          Every different class/struct/whatever returns a unique Type class with the GetType() function, right? Even when the object is cast into Object, it still returns the previous one, right? Now how do I see if that type is derived from some other thing? For example, if I have Object o and I need to see if it derives from ISomething interface. If System.Type is not for this and does soemthing completely different, what way could I do this?

          J Offline
          J Offline
          James T Johnson
          wrote on last edited by
          #4

          As Andres pointed out you can use the is/as statements to determine whether an object can be cast to the specified type name. In this case you use the name of the type, not the Type object representing that type. So for this to work you have to be able to reference the typename at compile time. If the types are loaded at runtime then you can't use the above methods, instead if you have two Type objects, one from each of the two variables then you can use something like this.

          bool IsBDerivedFromA(object a, object b)
          {
          Type ta = a.GetType();
          Type tb = b.GetType();

          // NOTE: Returns false if a and b are of the
          // same type, like two strings, two ints,
          // two Foos, etc
          return tb.IsSubclassOf(a);
          }

          James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

          L 1 Reply Last reply
          0
          • J James T Johnson

            As Andres pointed out you can use the is/as statements to determine whether an object can be cast to the specified type name. In this case you use the name of the type, not the Type object representing that type. So for this to work you have to be able to reference the typename at compile time. If the types are loaded at runtime then you can't use the above methods, instead if you have two Type objects, one from each of the two variables then you can use something like this.

            bool IsBDerivedFromA(object a, object b)
            {
            Type ta = a.GetType();
            Type tb = b.GetType();

            // NOTE: Returns false if a and b are of the
            // same type, like two strings, two ints,
            // two Foos, etc
            return tb.IsSubclassOf(a);
            }

            James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

            L Offline
            L Offline
            leppie
            wrote on last edited by
            #5

            I find IsAssignableFrom() better for most cases as it follows natural logic. :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

            J 1 Reply Last reply
            0
            • L leppie

              I find IsAssignableFrom() better for most cases as it follows natural logic. :) I rated this article 2 by mistake. It deserves more. I wanted to get to the second page... - vjedlicka 3:33 25 Nov '02

              J Offline
              J Offline
              James T Johnson
              wrote on last edited by
              #6

              leppie wrote: IsAssignableFrom() better for most cases as it follows natural logic Yep, I like that one better. I suggested IsSubclassOf because that was the only method I could remember while my MSDN was being laggy. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

              S 1 Reply Last reply
              0
              • J James T Johnson

                leppie wrote: IsAssignableFrom() better for most cases as it follows natural logic Yep, I like that one better. I suggested IsSubclassOf because that was the only method I could remember while my MSDN was being laggy. James "It is self repeating, of unknown pattern" Data - Star Trek: The Next Generation

                S Offline
                S Offline
                S O S
                wrote on last edited by
                #7

                Thank you :)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups