GetType for int64 returning System.Nullable

I have an object with a type property. When the type is set to Int64, and I try to pull the type information later, I get System.Nullable.

Here is type info

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

How do I get to type System.Int64?

+5
source share
5 answers

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

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

Since it Nullabe.GetUnderlyingTypereturns nullif this is Typenot Nullable<T>, you can use the null coalescing (??) operator for the default value for the source type.

+6
source
Type t = Nullable.GetUnderlyingType(nullableType);

. MSDN.

+1
if ( property.PropertyType.IsGenericType 
  && property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) )
+1
source

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

0
source

Perhaps you set your property type to "System.Int64?" anywhere? Otherwise, I cannot reproduce your behavior.

0
source

All Articles