Inner type of a Nullable type (C#)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Skychan
    New Member
    • Feb 2008
    • 3

    Inner type of a Nullable type (C#)

    I need to get the Inner Type of a Nullable Type (not an instance of given Nullable Type).

    For example, if I have:
    Type myType = typeof(int?);

    How can I determine that myType has an Inner Type of System.int32?

    Thanks
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by Skychan
    I need to get the Inner Type of a Nullable Type (not an instance of given Nullable Type).

    For example, if I have:
    Type myType = typeof(int?);

    How can I determine that myType has an Inner Type of System.int32?

    Thanks
    Have a look at the BaseType property of the Type class.

    Comment

    • Skychan
      New Member
      • Feb 2008
      • 3

      #3
      Originally posted by r035198x
      Have a look at the BaseType property of the Type class.
      typeof(int?).Ba seType returns:
      {Name = "ValueType" FullName = "System.ValueTy pe"} System.Type {System.Runtime Type}

      typeof(int?).Ba seType.BaseType returns:
      {Name = "Object" FullName = "System.Object" } System.Type {System.Runtime Type}

      I have looked through all the methods and properties of the BaseType but I can't find any information about int32.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by Skychan
        typeof(int?).Ba seType returns:
        {Name = "ValueType" FullName = "System.ValueTy pe"} System.Type {System.Runtime Type}

        typeof(int?).Ba seType.BaseType returns:
        {Name = "Object" FullName = "System.Object" } System.Type {System.Runtime Type}

        I have looked through all the methods and properties of the BaseType but I can't find any information about int32.
        Sorry about that. Use myType.FullName instead.

        Comment

        • Skychan
          New Member
          • Feb 2008
          • 3

          #5
          Here is the solution:

          determine if the Type is a nullable type and if it is then call Nullable's static method GetUnderlyingTy pe()



          That was unbelievably hard to find out and it makes no sense that to find out what the inner type is, a static method has to analyze the nullable type. It would make more sense for a nullable type to have a simple property of type Type containing the inner type.

          Comment

          Working...