Permalink
Browse files

Fix for Issue #131

BindingTypeConverters EqualityTypeConverter.DoReferenceCast doesn't
handle nullable values
  • Loading branch information...
1 parent 21d038c commit 56af2878e6c4f47c197617f7e8735c09d365b535 @robhorvath robhorvath committed with paulcbetts Dec 3, 2012
View
23 ReactiveUI.Tests/BindingTypeConvertersTest.cs
@@ -0,0 +1,23 @@
+using Xunit;
+
+namespace ReactiveUI.Tests
+{
+ public class BindingTypeConvertersTest
+ {
+ [Fact]
+ public void EqualityTypeConverterDoReferenceCastShouldConvertNullableValues()
+ {
+ double? nullableDouble = 0.0;
+ var result = EqualityTypeConverter.DoReferenceCast<double?>(nullableDouble);
+ Assert.Equal(nullableDouble, result);
+ }
+
+ [Fact]
+ public void EqualityTypeConverterDoReferenceCastShouldConvertValueTypes()
+ {
+ double doubleValue = 0.0;
+ var result = EqualityTypeConverter.DoReferenceCast<double>(doubleValue);
+ Assert.Equal(doubleValue, result);
+ }
+ }
+}
View
3 ReactiveUI.Tests/ReactiveUI.Tests.csproj
@@ -93,6 +93,7 @@
</CodeAnalysisDependentAssemblyPaths>
</ItemGroup>
<ItemGroup>
+ <Compile Include="BindingTypeConvertersTest.cs" />
<Compile Include="CommandBindingTests.cs" />
<Compile Include="DependencyObjectObservableForPropertyTest.cs" />
<Compile Include="ErrorsTest.cs" />
@@ -134,4 +135,4 @@
<Target Name="AfterBuild">
</Target>
-->
-</Project>
+</Project>
View
20 ReactiveUI/BindingTypeConverters.cs
@@ -42,16 +42,18 @@ public bool TryConvert(object from, Type toType, object conversionHint, out obje
public static object DoReferenceCast<T>(object from)
{
-#if WINRT
- bool isValueType = typeof (T).GetTypeInfo().IsValueType;
-#else
- bool isValueType = typeof (T).IsValueType;
-#endif
- if (isValueType) {
- return System.Convert.ChangeType(from, typeof (T), null);
- } else {
- return (T) from;
+ var t = typeof (T);
+ var u = Nullable.GetUnderlyingType(t);
+
+ if (u == null) {
+ return (T) Convert.ChangeType(from, t);
}
+
+ if (from == null) {
+ return default(T);
+ }
+
+ return (T) Convert.ChangeType(from, u);
}
}

0 comments on commit 56af287

Please sign in to comment.