From cc156c64635fff34af6584f5e28867e1e5f7d8f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Filip?= <34779382+tomasfil@users.noreply.github.com> Date: Tue, 19 Jul 2022 12:55:29 +0200 Subject: [PATCH] [FIX] nullable conversions (#3316) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fix nullable conversions * Fix when passing null, instead of string.Empty * Fix compiler error Co-authored-by: Tomáš Filip --- .../Converter/NullableByteToStringTypeConverter.cs | 12 ++++++++++++ .../NullableDecimalToStringTypeConverter.cs | 12 ++++++++++++ .../Converter/NullableDoubleToStringTypeConverter.cs | 12 ++++++++++++ .../NullableIntegerToStringTypeConverter.cs | 12 ++++++++++++ .../Converter/NullableLongToStringTypeConverter.cs | 12 ++++++++++++ .../Converter/NullableShortToStringTypeConverter.cs | 12 ++++++++++++ .../Converter/NullableSingleToStringTypeConverter.cs | 12 ++++++++++++ 7 files changed, 84 insertions(+) diff --git a/src/ReactiveUI/Bindings/Converter/NullableByteToStringTypeConverter.cs b/src/ReactiveUI/Bindings/Converter/NullableByteToStringTypeConverter.cs index 17c34561d5..9b1343d459 100644 --- a/src/ReactiveUI/Bindings/Converter/NullableByteToStringTypeConverter.cs +++ b/src/ReactiveUI/Bindings/Converter/NullableByteToStringTypeConverter.cs @@ -44,8 +44,20 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob return true; } + if (from is null) + { + result = null!; + return true; + } + if (from is string fromString) { + if (string.IsNullOrEmpty(fromString)) + { + result = null!; + return true; + } + var success = byte.TryParse(fromString, out var outByte); if (success) { diff --git a/src/ReactiveUI/Bindings/Converter/NullableDecimalToStringTypeConverter.cs b/src/ReactiveUI/Bindings/Converter/NullableDecimalToStringTypeConverter.cs index 6e9b8f7464..7c74ebde68 100644 --- a/src/ReactiveUI/Bindings/Converter/NullableDecimalToStringTypeConverter.cs +++ b/src/ReactiveUI/Bindings/Converter/NullableDecimalToStringTypeConverter.cs @@ -44,8 +44,20 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob return true; } + if (from is null) + { + result = null!; + return true; + } + if (from is string fromString) { + if (string.IsNullOrEmpty(fromString)) + { + result = null!; + return true; + } + var success = decimal.TryParse(fromString, out var outDecimal); if (success) { diff --git a/src/ReactiveUI/Bindings/Converter/NullableDoubleToStringTypeConverter.cs b/src/ReactiveUI/Bindings/Converter/NullableDoubleToStringTypeConverter.cs index d15203f8b4..1009e7086a 100644 --- a/src/ReactiveUI/Bindings/Converter/NullableDoubleToStringTypeConverter.cs +++ b/src/ReactiveUI/Bindings/Converter/NullableDoubleToStringTypeConverter.cs @@ -44,8 +44,20 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob return true; } + if (from is null) + { + result = null!; + return true; + } + if (from is string fromString) { + if (string.IsNullOrEmpty(fromString)) + { + result = null!; + return true; + } + var success = double.TryParse(fromString, out var outDouble); if (success) { diff --git a/src/ReactiveUI/Bindings/Converter/NullableIntegerToStringTypeConverter.cs b/src/ReactiveUI/Bindings/Converter/NullableIntegerToStringTypeConverter.cs index 8a3a7508ce..f6adca77c3 100644 --- a/src/ReactiveUI/Bindings/Converter/NullableIntegerToStringTypeConverter.cs +++ b/src/ReactiveUI/Bindings/Converter/NullableIntegerToStringTypeConverter.cs @@ -44,8 +44,20 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob return true; } + if (from is null) + { + result = null!; + return true; + } + if (from is string fromString) { + if (string.IsNullOrEmpty(fromString)) + { + result = null!; + return true; + } + var success = int.TryParse(fromString, out var outInt); if (success) { diff --git a/src/ReactiveUI/Bindings/Converter/NullableLongToStringTypeConverter.cs b/src/ReactiveUI/Bindings/Converter/NullableLongToStringTypeConverter.cs index dea8809ecb..b543b418fa 100644 --- a/src/ReactiveUI/Bindings/Converter/NullableLongToStringTypeConverter.cs +++ b/src/ReactiveUI/Bindings/Converter/NullableLongToStringTypeConverter.cs @@ -44,8 +44,20 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob return true; } + if (from is null) + { + result = null!; + return true; + } + if (from is string fromString) { + if (string.IsNullOrEmpty(fromString)) + { + result = null!; + return true; + } + var success = long.TryParse(fromString, out var outLong); if (success) { diff --git a/src/ReactiveUI/Bindings/Converter/NullableShortToStringTypeConverter.cs b/src/ReactiveUI/Bindings/Converter/NullableShortToStringTypeConverter.cs index acca63f8d2..63cce8e391 100644 --- a/src/ReactiveUI/Bindings/Converter/NullableShortToStringTypeConverter.cs +++ b/src/ReactiveUI/Bindings/Converter/NullableShortToStringTypeConverter.cs @@ -44,8 +44,20 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob return true; } + if (from is null) + { + result = null!; + return true; + } + if (from is string fromString) { + if (string.IsNullOrEmpty(fromString)) + { + result = null!; + return true; + } + var success = short.TryParse(fromString, out var outShort); if (success) { diff --git a/src/ReactiveUI/Bindings/Converter/NullableSingleToStringTypeConverter.cs b/src/ReactiveUI/Bindings/Converter/NullableSingleToStringTypeConverter.cs index f45af198f5..7b328ef627 100644 --- a/src/ReactiveUI/Bindings/Converter/NullableSingleToStringTypeConverter.cs +++ b/src/ReactiveUI/Bindings/Converter/NullableSingleToStringTypeConverter.cs @@ -44,8 +44,20 @@ public bool TryConvert(object? from, Type toType, object? conversionHint, out ob return true; } + if (from is null) + { + result = null!; + return true; + } + if (from is string fromString) { + if (string.IsNullOrEmpty(fromString)) + { + result = null!; + return true; + } + var success = float.TryParse(fromString, out var outSingle); if (success) {