AbstractNumericCalculator.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. using System.ComponentModel;
  2. using System.Globalization;
  3. using JetBrains.Annotations;
  4. namespace InABox.Avalonia.Converters;
  5. public abstract class AbstractNumericArrayConverter<TValue> : TypeConverter
  6. {
  7. protected abstract TValue[] Convert(IEnumerable<string> values);
  8. public override object ConvertFrom(
  9. ITypeDescriptorContext context, CultureInfo culture, object value)
  10. {
  11. if (value is string list)
  12. {
  13. try
  14. {
  15. var result = Convert(list.Split(','));
  16. return result;
  17. }
  18. catch
  19. {
  20. }
  21. }
  22. return new TValue[] { };
  23. }
  24. public override bool CanConvertFrom(
  25. ITypeDescriptorContext context, Type sourceType)
  26. {
  27. if (sourceType == typeof(string))
  28. return true;
  29. return base.CanConvertFrom(context, sourceType);
  30. }
  31. }
  32. public abstract class AbstractNumericCalculator<TValue,TFunction> : AbstractCalculator<TValue, TFunction, NumericCalculatorType>
  33. where TFunction : AbstractCalculatorFunction<TValue,NumericCalculatorType>, new()
  34. {
  35. [CanBeNull] public abstract TValue[] Constants { get; set; }
  36. protected override TValue Convert(IEnumerable<TValue> value, object parameter = null)
  37. {
  38. var enumerable = value as TValue[] ?? value.ToArray();
  39. var values = Constants != null
  40. ? Constants.Concat(enumerable.ToArray())
  41. : enumerable;
  42. return base.Convert(values, parameter);
  43. }
  44. }