IsNullable
Returns true if the type it is called upon is a System.Nullable<> wrapped type.
Source
using System;
using System.Collections.Generic;
using System.Linq;
/// <summary>
/// Extension methods for the Type class
/// </summary>
public static class TypeExtensions
{
/// <summary>
/// Return true if the type is a System.Nullable wrapper of a value type
/// </summary>
/// <param name="type">The type to check</param>
/// <returns>True if the type is a System.Nullable wrapper</returns>
public static bool IsNullable(this Type type)
{
return type.IsGenericType
&& (type.GetGenericTypeDefinition() == typeof(Nullable<>));
}
}
Example
// false
if (typeof(int).IsNullable())
{
...
}
// true
if (typeof(int?).IsNullable())
{
...
}
Author: James Michael Hare (BlackRabbitCoder)
Submitted on: 14 okt. 2010
Language: C#
Type: System.Type
Views: 9151