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

  • Thread starter Thread starter Narshe
  • Start date Start date
N

Narshe

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
 
Yes, not all value types are nullable though.

Would this work?

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

Where field is of type FieldInfo.
 
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.
 
Back
Top