At a glance,
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));
}
}
When calling a Read<T>
function
var myVariable = Conversion.Read<bool?>(Row[nameof(IsFetchNextRecordAfterDelete)]);
Error CS0452 The type 'bool?' must be a reference type in order to use it as parameter 'T' in the generic type or method 'Conversion.Read(object)'
Wonder why? the bool? is nullable that's mean its a reference type, and generic method declared where T : class
'bool?' is not a reference type. It is a nullable value type. see Nullable value types (C# reference) The underlying type is a struct (which is a value type).
where T :class
This constraint means that the type argument that you will provide will be a reference type that includes any class,interface, delegate, or array type. Type Constraints
Nullable-types come under the category of value types not as reference.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With