# Sunday, March 02, 2008

Working with nullable types using reflection is not that hard, but sometimes you have to think about it for a bit or browse the help file before you can actually use it correctly.

What I will list here is not rocket science, but it could be helpful to have all the nullable-related stuff in one short article.

Get the acutal (non-nullable) type of nullable (if it's not nullable, the type itself is returned):

Type realType = Nullable.GetUnderlyingType(type) ?? type;

Determine if a type is a nullable type:

bool isNullable = type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>);

Create a generic type based on a non-nullable type:

Type nullableType = typeof(Nullable<>).MakeGenericType(type);

Finally, get the type of the elements of an array type:

Type elementType = arrayType.GetElementType();

 

As I said before, everyone could find the answers in the .NET documentation relatively quickly, but this quick reference could come in handy. If not, just ignore me :-)

kick it on DotNetKicks.com
Sunday, March 02, 2008 11:05:38 AM (W. Europe Standard Time, UTC+01:00)  #    Comments [0] -

Comments are closed.