AbstractNumericCalculator.cs 1.7 KB

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