using System;
namespace Artemis.Core;
///
public class DataBindingProperty : IDataBindingProperty
{
internal DataBindingProperty(Func getter, Action setter, string displayName)
{
Getter = getter ?? throw new ArgumentNullException(nameof(getter));
Setter = setter ?? throw new ArgumentNullException(nameof(setter));
DisplayName = displayName ?? throw new ArgumentNullException(nameof(displayName));
}
///
/// Gets the function to call to get the value of the property
///
public Func Getter { get; }
///
/// Gets the action to call to set the value of the property
///
public Action Setter { get; }
///
public string DisplayName { get; }
///
public Type ValueType => typeof(TProperty);
///
public object? GetValue()
{
return Getter();
}
///
public void SetValue(object value)
{
// Numeric has a bunch of conversion, this seems the cheapest way to use them :)
switch (value)
{
case TProperty match:
Setter(match);
break;
case Numeric numeric when Setter is Action floatSetter:
floatSetter(numeric);
break;
case Numeric numeric when Setter is Action intSetter:
intSetter(numeric);
break;
case Numeric numeric when Setter is Action doubleSetter:
doubleSetter(numeric);
break;
case Numeric numeric when Setter is Action byteSetter:
byteSetter(numeric);
break;
default:
throw new ArgumentException("Value must match the type of the data binding registration", nameof(value));
}
}
}