473,396 Members | 2,023 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,396 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 4640
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
by: Yuriy Solodkyy | last post by:
Hi VS 2005 beta 2 successfully compiles the following: using System; using System.Collections.Generic; using System.Text; namespace ConsoleApplication1 { class Program {
10
by: Henri | last post by:
In java for instance there's a way to use booleans as objects and not as value types. I would like to do the same in VB.NET so that I can check if the boolean has been explicitely defined (is not...
2
by: Nigel Sampson | last post by:
Hi, I'm in the process of upgrading an Object-Relational mapping library to .NET 2.0 and am wanting to implement support for Nullable typess. I'm using Reflection to analyise the object that is...
2
by: Andrew Robinson | last post by:
I am guessing there is a simple solution but given a type T, how can I check for nullability? how can I accomplish the following? bool nullable = typeof(int).IsNullable; // false bool...
4
by: shapper | last post by:
Hello, Inside a class I have 4 propertiy types: 1. String 2. Boolean 3. Mail.MailPriority
1
by: Dan Holmes | last post by:
i need to get an object of type Type when i have a string. That is the easy part (Type.GetType("int")). What if i want that type to be nullable? This doesn't work Type.GetType("int?"). It...
2
by: Andrus | last post by:
My type contains constructor with nullable parameter like public class Iandmed { public Iandmed(DateTime? miskuup) {} } I tried to get its constructor parameter type name using Type t =...
4
by: RP | last post by:
I have a member variable of a class of type "int", and while assigning value to it in a constructor, I want to check whether it is a null. I am using following code: if (NewMember.BPLNo == null)...
6
by: Andrus | last post by:
I need to create method which returns my application object type as string : TypeName(typeof( List<int>)) should return "List<int>" or "System.Collections.Generic.List<int>" ...
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:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.