AbstractNumericCalculator.cs 1.8 KB

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