我有一个具有 type 属性的对象。当类型设置为 Int64 时,我稍后尝试提取类型信息,我得到 System.Nullable。
这是类型信息
{Name = "Nullable`1" FullName = "System.Nullable`1[[System.Int64, mscorlib, Version=2.0.0.0]]"}
如何获得 System.Int64 类型的这个?
听起来您有一个给定的类型,您想要该类型或其基础类型(如果它是Nullable<T>
. 最好的方法是这样的:
Nullable.GetUnderlyingType(yourObject.Type) ?? yourObject.Type;
因为如果给定不是Nullabe.GetUnderlyingType
返回,您可以使用空合并运算符 (??) 将值默认为原始类型。null
Type
Nullable<T>
Type t = Nullable.GetUnderlyingType(nullableType);
请参阅MSDN。
if ( property.PropertyType.IsGenericType
&& property.PropertyType.GetGenericTypeDefinition() == typeof(Nullable<>) )
如果对象不为空,GetType 将返回 Int64。
也许您将类型属性设置为“System.Int64”?任何地方?否则我无法重现你的行为。