Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GetType of int64 returning System.Nullable

I've got an object that has a type property. When the type is set to Int64, and I try and pull the type info later, I get System.Nullable.

Here's the type Info

{Name = "Nullable`1" FullName = "System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0]]"}

How do I get to the System.Int64 type of this?

like image 393
JoelHess Avatar asked Feb 19 '09 14:02

JoelHess


2 Answers

It sounds like you have a given type for which you want either that type or its underlying type if it is Nullable<T>. The best way to do this would be something like this:

Nullable.GetUnderlyingType(yourObject.Type) ?? yourObject.Type;

Since Nullabe.GetUnderlyingType returns null if the given Type is not Nullable<T> you can use the null coalescing operator (??) to default the value to the original type.

like image 151
Andrew Hare Avatar answered Sep 22 '22 19:09

Andrew Hare


Type t = Nullable.GetUnderlyingType(nullableType);

See MSDN.

like image 43
jason Avatar answered Sep 20 '22 19:09

jason