Nullable detection kludge

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • jon.rea@gmail.com

    Nullable detection kludge

    Please tell me there is a better way :-D

    ....

    public static bool IsNullableType( Type paramType)
    {
    return paramType.IsGen ericType &&
    paramType.GetGe nericTypeDefini tion() == typeof
    (Nullable<>);
    }

    public static bool IsNullableTypeM atch(Type paramType, Type
    argType)
    {
    return IsNullableType( paramType) &&
    argType.IsValue Type &&
    paramType.FullN ame.Contains(ar gType.FullName) ; //
    **********Kludg e***********
    }

    - paramType {Name = "Nullable`1 " FullName = "System.Nullabl e`1
    [[System.Int32, mscorlib, Version=2.0.0.0 , Culture=neutral ,
    PublicKeyToken= b77a5c561934e08 9]]"} System.Type {System.Runtime Type}

    - argType {Name = "Int32" FullName = "System.Int 32"} System.Type
    {System.Runtime Type}

    IsNullableTypeM atch(...) -->

    Nullable<Int32w ith Int32 -- returns true
    Nullable<OtherV alueTypewith Int32 -- returns false
    Nullable<Int32w ith OtherValueType -- returns false

    Any ideas how I can remove my kludge???

    Many thanks for any help,
    Jon
  • Hans Kesting

    #2
    Re: Nullable detection kludge

    jon.rea@gmail.c om submitted this idea :
    Please tell me there is a better way :-D
    >
    ...
    >
    public static bool IsNullableType( Type paramType)
    {
    return paramType.IsGen ericType &&
    paramType.GetGe nericTypeDefini tion() == typeof
    (Nullable<>);
    }
    >
    public static bool IsNullableTypeM atch(Type paramType, Type
    argType)
    {
    return IsNullableType( paramType) &&
    argType.IsValue Type &&
    paramType.FullN ame.Contains(ar gType.FullName) ; //
    **********Kludg e***********
    }
    >
    - paramType {Name = "Nullable`1 " FullName = "System.Nullabl e`1
    [[System.Int32, mscorlib, Version=2.0.0.0 , Culture=neutral ,
    PublicKeyToken= b77a5c561934e08 9]]"} System.Type {System.Runtime Type}
    >
    - argType {Name = "Int32" FullName = "System.Int 32"} System.Type
    {System.Runtime Type}
    >
    IsNullableTypeM atch(...) -->
    >
    Nullable<Int32w ith Int32 -- returns true
    Nullable<OtherV alueTypewith Int32 -- returns false
    Nullable<Int32w ith OtherValueType -- returns false
    >
    Any ideas how I can remove my kludge???
    >
    Many thanks for any help,
    Jon

    This should work:

    public static bool IsNullableTypeM atch(Type paramType, Type argType)
    {
    return IsNullableType( paramType) &&
    argType.IsValue Type &&
    paramType.GetGe nericArguments( )[0] == argType;
    }

    Hans Kesting


    Comment

    • Ben Voigt [C++ MVP]

      #3
      Re: Nullable detection kludge

      Hans Kesting wrote:
      jon.rea@gmail.c om submitted this idea :
      >Please tell me there is a better way :-D
      >>
      >...
      >>
      > public static bool IsNullableType( Type paramType)
      > {
      > return paramType.IsGen ericType &&
      > paramType.GetGe nericTypeDefini tion() == typeof
      >(Nullable<>) ;
      > }
      >>
      > public static bool IsNullableTypeM atch(Type paramType, Type
      >argType)
      > {
      > return IsNullableType( paramType) &&
      > argType.IsValue Type &&
      > paramType.FullN ame.Contains(ar gType.FullName) ; //
      >**********Klud ge***********
      > }
      >>
      >- paramType {Name = "Nullable`1 " FullName = "System.Nullabl e`1
      >[[System.Int32, mscorlib, Version=2.0.0.0 , Culture=neutral ,
      >PublicKeyToken =b77a5c561934e0 89]]"} System.Type {System.Runtime Type}
      >>
      >- argType {Name = "Int32" FullName = "System.Int 32"} System.Type
      >{System.Runtim eType}
      >>
      >IsNullableType Match(...) -->
      >>
      >Nullable<Int32 with Int32 -- returns true
      >Nullable<Other ValueTypewith Int32 -- returns false
      >Nullable<Int32 with OtherValueType -- returns false
      >>
      >Any ideas how I can remove my kludge???
      >>
      >Many thanks for any help,
      >Jon
      >
      >
      This should work:
      >
      public static bool IsNullableTypeM atch(Type paramType, Type argType)
      {
      return IsNullableType( paramType) &&
      argType.IsValue Type &&
      the IsValueType check here is unnecessary
      paramType.GetGe nericArguments( )[0] == argType;
      }
      >
      Hans Kesting

      Comment

      Working...