Nullable detection kludge

J

jon.rea

Please tell me there is a better way :-D

....

public static bool IsNullableType(Type paramType)
{
return paramType.IsGenericType &&
paramType.GetGenericTypeDefinition() == typeof
(Nullable<>);
}

public static bool IsNullableTypeMatch(Type paramType, Type
argType)
{
return IsNullableType(paramType) &&
argType.IsValueType &&
paramType.FullName.Contains(argType.FullName); //
**********Kludge***********
}

- paramType {Name = "Nullable`1" FullName = "System.Nullable`1
[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]"} System.Type {System.RuntimeType}

- argType {Name = "Int32" FullName = "System.Int32"} System.Type
{System.RuntimeType}

IsNullableTypeMatch(...) -->

Nullable<Int32> with Int32 -- returns true
Nullable<OtherValueType> with Int32 -- returns false
Nullable<Int32> with OtherValueType -- returns false

Any ideas how I can remove my kludge???

Many thanks for any help,
Jon
 
H

Hans Kesting

(e-mail address removed) submitted this idea :
Please tell me there is a better way :-D

...

public static bool IsNullableType(Type paramType)
{
return paramType.IsGenericType &&
paramType.GetGenericTypeDefinition() == typeof
(Nullable<>);
}

public static bool IsNullableTypeMatch(Type paramType, Type
argType)
{
return IsNullableType(paramType) &&
argType.IsValueType &&
paramType.FullName.Contains(argType.FullName); //
**********Kludge***********
}

- paramType {Name = "Nullable`1" FullName = "System.Nullable`1
[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]"} System.Type {System.RuntimeType}

- argType {Name = "Int32" FullName = "System.Int32"} System.Type
{System.RuntimeType}

IsNullableTypeMatch(...) -->

Nullable<Int32> with Int32 -- returns true
Nullable<OtherValueType> with 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 IsNullableTypeMatch(Type paramType, Type argType)
{
return IsNullableType(paramType) &&
argType.IsValueType &&
paramType.GetGenericArguments()[0] == argType;
}

Hans Kesting
 
B

Ben Voigt [C++ MVP]

Hans said:
(e-mail address removed) submitted this idea :
Please tell me there is a better way :-D

...

public static bool IsNullableType(Type paramType)
{
return paramType.IsGenericType &&
paramType.GetGenericTypeDefinition() == typeof
(Nullable<>);
}

public static bool IsNullableTypeMatch(Type paramType, Type
argType)
{
return IsNullableType(paramType) &&
argType.IsValueType &&
paramType.FullName.Contains(argType.FullName); //
**********Kludge***********
}

- paramType {Name = "Nullable`1" FullName = "System.Nullable`1
[[System.Int32, mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089]]"} System.Type {System.RuntimeType}

- argType {Name = "Int32" FullName = "System.Int32"} System.Type
{System.RuntimeType}

IsNullableTypeMatch(...) -->

Nullable<Int32> with Int32 -- returns true
Nullable<OtherValueType> with 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 IsNullableTypeMatch(Type paramType, Type argType)
{
return IsNullableType(paramType) &&
argType.IsValueType &&

the IsValueType check here is unnecessary
paramType.GetGenericArguments()[0] == argType;
}

Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top