TimePeriodsConverter.cs 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. namespace IndexFormula.Finance
  2. {
  3. using System;
  4. using System.ComponentModel;
  5. using System.Globalization;
  6. public class TimePeriodsConverter : ArrayConverter
  7. {
  8. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  9. {
  10. return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
  11. }
  12. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  13. {
  14. return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
  15. }
  16. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  17. {
  18. if (value is string)
  19. {
  20. string[] strArray = (value as string).Split(new char[] { ',' });
  21. if (strArray.Length > 0)
  22. {
  23. TimePeriod[] periodArray = new TimePeriod[strArray.Length];
  24. TypeConverter converter = TypeDescriptor.GetConverter(typeof(TimePeriod));
  25. for (int i = 0; i < strArray.Length; i++)
  26. {
  27. periodArray[i] = (TimePeriod) converter.ConvertFromString(strArray[i]);
  28. }
  29. return periodArray;
  30. }
  31. }
  32. return base.ConvertFrom(context, culture, value);
  33. }
  34. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  35. {
  36. if ((destinationType == typeof(string)) && (value is TimePeriod[]))
  37. {
  38. string str = "";
  39. foreach (TimePeriod period in value as TimePeriod[])
  40. {
  41. if (str != "")
  42. {
  43. str = str + ",";
  44. }
  45. str = str + period.ToString();
  46. }
  47. return str;
  48. }
  49. return base.ConvertTo(context, culture, value, destinationType);
  50. }
  51. }
  52. }