一目了然,
public static class Conversion
{
public static T Read<T>(object value) where T :class
{
if (value is DBNull) return null;
if (value is null) return null;
if (value is Enum) return (T)Enum.Parse(typeof(T), value.ToString(), true);
return (T)Convert.ChangeType(value, typeof(T));
}
}
当 调用
Read<T>
函数时var myVariable = Conversion.Read<bool?>(Row[nameof(IsFetchNextRecordAfterDelete)]);
不知道为什么? bool 值?可以为空,这意味着它是一个引用类型,并且泛型方法声明为
where T : class
最佳答案
“ bool ?”是 不是 引用类型。它是一个可为空的 值 类型。
见 Nullable value types (C# reference)
底层类型是一个结构体(它是一个值类型)。
关于c# - 为什么接受引用类型的泛型方法不接受可空类型作为参数?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58864236/