473,386 Members | 1,798 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Nullable detection kludge

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<Int32with Int32 -- returns true
Nullable<OtherValueTypewith Int32 -- returns false
Nullable<Int32with OtherValueType -- returns false

Any ideas how I can remove my kludge???

Many thanks for any help,
Jon
Nov 17 '08 #1
2 2496
jo*****@gmail.com 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<Int32with Int32 -- returns true
Nullable<OtherValueTypewith Int32 -- returns false
Nullable<Int32with 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
Nov 17 '08 #2
Hans Kesting wrote:
jo*****@gmail.com 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<Int32with Int32 -- returns true
Nullable<OtherValueTypewith Int32 -- returns false
Nullable<Int32with 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

Nov 17 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: ESPNSTI | last post by:
Hi, Please don't shoot me if the answer is simple, I'm new to .Net and C# :) .. I'm attempting to convert a nullable type to it's "non-nullable" type in a generic way (without knowing what...
8
by: shawnk | last post by:
Given several nullable boolean flags; bool? l_flg_01 = true; bool? l_flg_02 = false; bool? l_flg_03 = true; bool? l_result_flg = null; I would have liked...
0
by: Larry Lard | last post by:
There seems to be something a bit lacking in the way the dataset designer thing deals (or rather doesn't) with nullable fields in VS2005. Maybe it's cos I'm using VB2005 Express (which is variously...
3
by: rubikzube* | last post by:
Hi. I'm trying to write a snippet of code to detect if a variable is a nullable<t> struct, and I can't figure out the right syntax or if such a thing is even possible... Below are the results...
1
by: Joe Bloggs | last post by:
Hi, Can someone please kindly show me how to determine if a type (read value type) is Nullable. MSDN has this KB: How to: Identify a Nullable Type (C# Programming Guide)...
5
by: GG | last post by:
I am trying to add a nullable datetime column to a datatable fails. I am getting exception DataSet does not support System.Nullable<>. None of these works dtSearchFromData.Columns.Add( new...
8
by: Sam Kong | last post by:
Hello, I want to define a generic class which should accept only nullable types or reference types. What's the best way to costrain it? --------- class MyClass<T>{ ...
9
by: MLM450 | last post by:
Is there a way to get the address of the underlying value of a nullable variable without copying the value? I tried the following, but the compiler doesn't like it. I need to do this because I am...
2
by: Tony Johansson | last post by:
Hello! These two declarations(1 and 2) are the same I assume. 1. System.Nullable<intnullable; 2. System.Nullable<intnullable = new System.Nullable<int(); So because these 1 and 2 are the...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.