Convert.ChangeType() method may fail when converting a Nullable type in C# if the value passed to it is null. This is because Nullable types are wrapped in a container object and cannot be boxed or unboxed directly like non-nullable types.
To work around this, you can first check if the value is null and handle it accordingly before calling Convert.ChangeType(). You can use the Nullable.GetUnderlyingType() method to check if a type is nullable and get its underlying type.
Here's an example:
object value = null; Type targetType = typeof(int?); if (Nullable.GetUnderlyingType(targetType) != null && value == null) { // The target type is nullable and the value is null // Handle this case accordingly // For example, you can set the value to null value = null; } else { // The target type is not nullable or the value is not null // Call Convert.ChangeType() to perform the conversion value = Convert.ChangeType(value, targetType); } In this example, value is the value you want to convert, and targetType is the target type you want to convert to, which is a nullable int in this case (typeof(int?)). The Nullable.GetUnderlyingType() method is used to check if the target type is nullable, and if so, the value is set to null. Otherwise, Convert.ChangeType() is called to perform the conversion as usual.
Note that this approach can be extended to handle other scenarios where Convert.ChangeType() might fail, such as converting to or from custom types.
"Convert.ChangeType fails with nullable int"
int? nullableInt = 42; object result = Convert.ChangeType(nullableInt, typeof(int));
Convert.ChangeType() fails when trying to convert a nullable int to a non-nullable int."Convert.ChangeType fails with nullable DateTime"
DateTime? nullableDateTime = DateTime.Now; object result = Convert.ChangeType(nullableDateTime, typeof(DateTime));
Convert.ChangeType() fails when trying to convert a nullable DateTime to a non-nullable DateTime."C# Convert.ChangeType fails with nullable type and default value"
int? nullableInt = null; object result = Convert.ChangeType(nullableInt, typeof(int), defaultValue: 0);
defaultValue parameter in Convert.ChangeType()."Convert.ChangeType fails with nullable type and string conversion"
int? nullableInt = 42; object result = Convert.ChangeType(nullableInt.ToString(), typeof(int));
"Convert.ChangeType fails with nullable enum"
MyEnum? nullableEnum = MyEnum.Value; object result = Convert.ChangeType(nullableEnum, typeof(MyEnum));
Convert.ChangeType() fails when converting a nullable enum to a non-nullable enum."C# Convert.ChangeType fails with nullable type and DBNull.Value"
int? nullableInt = 42; object result = Convert.ChangeType(DBNull.Value, typeof(int?));
Convert.ChangeType() can fail when trying to convert DBNull.Value to a nullable type."Convert.ChangeType fails with nullable type and custom conversion"
int? nullableInt = 42; object result = Convert.ChangeType(nullableInt, typeof(int), new CustomConverter());
Convert.ChangeType()."C# Convert.ChangeType fails with nullable type and IConvertible implementation"
int? nullableInt = 42; IConvertible convertibleValue = nullableInt; object result = Convert.ChangeType(convertibleValue, typeof(int));
IConvertible on a nullable type may not prevent Convert.ChangeType() from failing."Convert.ChangeType fails with nullable type and non-convertible target type"
int? nullableInt = 42; object result = Convert.ChangeType(nullableInt, typeof(string));
Convert.ChangeType() fails when converting a nullable int to a non-convertible target type."C# Convert.ChangeType fails with nullable type and dynamic conversion"
int? nullableInt = 42; dynamic result = Convert.ChangeType(nullableInt, typeof(int));
Convert.ChangeType() can still lead to failures when handling nullable types.android-support-design backwards-compatibility strcat image-resizing plc autostart laravel-query-builder retain-cycle mathjax brackets