Fieldinfo.FieldType.FullName 未提供日期时间和双精度的数据类型

Pra*_*ady 2 c# reflection visual-studio-2005

我正在使用 Fieldinfo.FieldType.FullName 来获取字段数据类型。对于字符串我得到 System.String 但对于 Double 我得到

System.Nullable`1[[System.Double、mscorlib、版本=2.0.0.0、文化=中性、PublicKeyToken=b77a5c561934e089]

类似地,对于 DateTime 我得到 System.Nullable`1[[System.DateTime, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]

我需要检查我的代码,如果这个特定字段是日期时间,然后执行某些操作。我如何检查该字段是双精度型、字符串型还是双精度型等

提前致谢

Kir*_*oll 5

JaredPar 的回答是一个很好的解释。如需解决方案,请尝试:

Type fieldType = fieldInfo.FieldType;
if (fieldType.IsGenericType && fieldType.GetGenericTypeDefinition() == typeof(Nullable<>))
    fieldType = fieldType.GetGenericArguments()[0];
Run Code Online (Sandbox Code Playgroud)

现在你可以说:

if (fieldType == typeof(double))
    ...
Run Code Online (Sandbox Code Playgroud)

等等。此代码基本上将“撤消”可空类型语义。