views:

499

answers:

5

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?

+1  A: 
Type t = Nullable.GetUnderlyingType(nullableType);

See MSDN.

Jason
A: 

If the object is not null, GetType will return Int64.

Jimbo
A: 

Maybe you set your type-property to "System.Int64?" anywhere? Otherwise I can not reproduce your behaviour.

tanascius
Same for me... Strange behaviour that can certainly be explained by an information we don't have ;)
ybo
+2  A: 

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` you can use the null coalescing operator (??) to default the value to the original type.

Andrew Hare
A: 

For using System.Nullable please refer Nullable type in C# brief details

bimbim.in