gpt4 book ai didi

c# - Fieldinfo.FieldType.FullName 没有为 datetime 和 double 提供数据类型

转载 作者:太空宇宙 更新时间:2023-11-03 20:39:40 25 4
gpt4 key购买 nike

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

System.Nullable`1[[System.Double, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]

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

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

提前致谢

最佳答案

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

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

现在你可以说:

if (fieldType == typeof(double))
...

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

关于c# - Fieldinfo.FieldType.FullName 没有为 datetime 和 double 提供数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3636528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com