473,618 Members | 3,044 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 4669
The Type class has a IsValueType property to tell you this.

"Narshe" <na****@gmail.c om> wrote in message
news:11******** **************@ g14g2000cwa.goo glegroups.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.co m

"Narshe" <na****@gmail.c om> wrote in message
news:11******** **************@ z14g2000cwz.goo glegroups.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
5630
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
13813
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 Nothing). But Boolean is a Structure in VB.NET (defined to False by default) and not an Object so I'm afraid there's no way to do what I want without any workaround. Can you confirm that there's no object version of Boolean in VB.NET? Thanks
2
1899
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 being mapped and building up a schema for the mapping. This means I need a way to determine if the PropertyInfo.PropertyType is an implementation of System.Nullable<>. I've been batting my head against this for a while and only come up with one...
2
2335
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 nullable = typeof(int?).IsNullable; // true bool nullable = typeof(string).IsNullable; // true Thanks for any help.
4
9272
by: shapper | last post by:
Hello, Inside a class I have 4 propertiy types: 1. String 2. Boolean 3. Mail.MailPriority
1
2494
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 returns null. This does work but i don't know how to know all that information at runtime. Type.GetType("System.Nullable`1]") dan
2
7923
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 = typeof(Iandmed); ConstructorInfo c = t.GetConstructors();
4
5087
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) NewMember.BPLNo = 0; But, Visual Studio 2005 is giving warning that variables with type 'int' cannot be 'null'. How to check this?
6
2408
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>" TypeName(typeof( MyType<Customer>)) should return "MyNamesp.MyType<MyBusiness.Customer>" etc.
0
8212
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8153
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8455
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7126
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6101
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4065
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1459
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.