Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Moryx/Bindings/ReflectionResolver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ protected sealed override bool Update(object source, object value)
if (property == null || !property.CanWrite)
return false;

if (value is string && property.PropertyType.IsPrimitive)
if (value is IConvertible && property.PropertyType.GetInterfaces().Contains(typeof(IConvertible)) )
property.SetValue(source, Convert.ChangeType(value, property.PropertyType));
else
property.SetValue(source, value);
Expand Down
18 changes: 18 additions & 0 deletions src/Tests/Moryx.Tests/Bindings/ReflectionResolverTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) 2020, Phoenix Contact GmbH & Co. KG
// Licensed under the Apache License, Version 2.0

using System;
using System.Reflection;
using Moryx.Bindings;
using NUnit.Framework;
Expand Down Expand Up @@ -63,6 +64,23 @@ public void AssignStringToInt()
Assert.AreEqual(int.Parse(number), obj.SimpleInt);
}

[Test]
public void AssignDoubleToString()
{
const double value = 7.78;

// Arrange
var obj = new SomeHiddenPropertyClass();
IBindingResolver reflectionResolver = new ReflectionResolver(nameof(SomeClass.SimpleString));

// Act
var result = reflectionResolver.Update(obj, value);

// Assert
Assert.IsTrue(result);
Assert.AreEqual(value, Convert.ToDouble(obj.SimpleString));
}

[Test(Description = "Checks if the reflection resolver returns null by resolving an unknown property")]
public void NullByUnknownProperty()
{
Expand Down