c# - 如何检测与可空属性结合的数据类型

标签 c# .net reflection nullable

我正在使用反射来检索通用类对象属性,通过检测它们的数据类型(例如 System.String、System.DateTime 等)并根据数据类型转换值,例如:

switch (prop.PropertyType.FullName)
{
    case "System.String":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            string.Empty : Convert.ToString(_propertyDataValue));
        break;
    case "System.Int32":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            -1 : Convert.ToInt32(_propertyDataValue));
        break;
    case "System.DateTime":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            DateTime.MinValue : Convert.ToDateTime(_propertyDataValue));
        break;
    case "System.Double":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            0 : Convert.ToDouble(_propertyDataValue));
        break;
    case "System.Boolean":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            false : Convert.ToBoolean(_propertyDataValue));
        break;
    default:
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            string.Empty : Convert.ToString(_propertyDataValue));
        break;
}

但是,当我遇到属性定义为int?,double?或 DateTime? 这将是一个 Nullable 类型,我无法弄清楚该属性的确切数据类型,他的反射只给我类型为“System.Nullable”,是否存在无论如何要检测后面的组合数据类型?

最佳答案

作为@madreflection评论区提到。您需要使用 Nullable.GetUnderlyingType如下:

var propType = prop.PropertyType;

if (Nullable.GetUnderlyingType(propType) != null)
{
    // It's nullable
    propType = Nullable.GetUnderlyingType(propType);
}

switch (propType.FullName)
{
    case "System.String":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            string.Empty : Convert.ToString(_propertyDataValue));
        break;
    case "System.Int32":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            -1 : Convert.ToInt32(_propertyDataValue));
        break;
    case "System.DateTime":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            DateTime.MinValue : Convert.ToDateTime(_propertyDataValue));
        break;
    case "System.Double":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            0 : Convert.ToDouble(_propertyDataValue));
        break;
    case "System.Boolean":
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            false : Convert.ToBoolean(_propertyDataValue));
        break;
    default:
        prop.SetValue(resultObject, _propertyDataValue == DBNull.Value ?
            string.Empty : Convert.ToString(_propertyDataValue));
        break;
}

关于c# - 如何检测与可空属性结合的数据类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59362538/

相关文章:

c# - 删除 xml 节点 -Xelement

c# - 当 .NET 只依赖于参数时,我应该使用委托(delegate)还是方法?

c# - .NET 进程打开没有选项卡或地址栏的网页

c# - 从 SortedList 或 SortedDictionary 获取第 i 个值

java - 如何在类路径中找到接口(interface)的所有实现?

c# - 对象和对象列表的类型参数约束

c# - 使用 Canvas 矩形裁剪图像

.NET ResourceManager.GetResourceSet : "en-GB" not working on english resource

c# - 用反射检测原生对象

c# - 检查两个对象的属性以进行更改