471,318 Members | 2,655 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

How can I check if a type is nullable with reflection?

How can I check if a type is nullable?

If a type is created like bool? = null;, then how can you tell if the
type is nullable as opposed to not? You have to cast from bool to bool?
and such, so I'll need to know if a type is nullable.

-Josh

Nov 28 '05 #1
5 4381
The Type class has a IsValueType property to tell you this.

"Narshe" <na****@gmail.com> wrote in message
news:11**********************@g14g2000cwa.googlegr oups.com...
How can I check if a type is nullable?

If a type is created like bool? = null;, then how can you tell if the
type is nullable as opposed to not? You have to cast from bool to bool?
and such, so I'll need to know if a type is nullable.

-Josh

Nov 28 '05 #2
Yes, not all value types are nullable though.

Would this work?

if( field.FieldType == typeof( Nullable<bool> ) ){}

Where field is of type FieldInfo.

Nov 28 '05 #3
Narshe,

That would work, but that would tell you that only if the type is
nullable of bool, not nullable in general.

Also, ANY structure can be used in Nullable<T>. The only requirement is
that T is a structure.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Narshe" <na****@gmail.com> wrote in message
news:11**********************@z14g2000cwz.googlegr oups.com...
Yes, not all value types are nullable though.

Would this work?

if( field.FieldType == typeof( Nullable<bool> ) ){}

Where field is of type FieldInfo.

Nov 28 '05 #4
Thanks.

Nov 28 '05 #5
Hi Narshe,
Not sure if anyone is still reading this thread, but check out my
article it might help you a bit.

http://www.developersdex.com/gurus/articles/739.asp
Happy Coding,

Stefan
C# GURU
www.DotNETovation.com

"You always have to look beyond the horizon and can never be complacent
-- God forbid we become complacent."

Jozef Straus

*** Sent via Developersdex http://www.developersdex.com ***

Nov 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

27 posts views Thread by Yuriy Solodkyy | last post: by
2 posts views Thread by Nigel Sampson | last post: by
2 posts views Thread by Andrew Robinson | last post: by
4 posts views Thread by shapper | last post: by
1 post views Thread by Dan Holmes | last post: by
2 posts views Thread by Andrus | last post: by
4 posts views Thread by RP | last post: by
6 posts views Thread by Andrus | last post: by
reply views Thread by rosydwin | last post: by

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.