Kevin McNeish Blog

All things iOS, Kindle and .NET

Recent Posts

Tags

News

  • First books in my new book series, "iOS App Development for Non-Programmers" are now available! iBookStore: http://itunes.apple.com/us/book/book-1-diving-in-ios-app-development/id558788074?mt=11 Amazon: http://www.amazon.com/dp/B0097N8XBE Amazon: http://www.amazon.com/dp/B0099RQGMQ

Community

Email Notifications

Archives

Using Reflection to Determine if a Property Data Type is Nullable

You can easily determine if the data type of an object property is nullable using the following code:

Type t = objectReference.GetType();
Type pt;

// Test for Nullable
bool isNullable = pi.PropertyType.IsGenericType && pi.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>);
PropertyInfo pi = t.GetProperty("MyPropertyName");

if (isNullable)
{
   // Returns the basic data type without reference to Nullable (for example, System.Int32)
   pt = pi.PropertyType.GetGenericArguments()[0];
}

Best Regards,
Kevin McNeish
Chief Architect, MM .NET Application Framework
INETA Speaker
.NET MVP 2002-2009
www.oakleafsd.com 

Leave a Comment

(required) 

(required) 

(optional)
 

(required) 

If you can't read this number refresh your screen
Enter the numbers above: