| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- namespace IndexFormula.Finance
- {
- using System;
- using System.ComponentModel;
- using System.Globalization;
- public class TimePeriodsConverter : ArrayConverter
- {
- public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
- {
- return ((sourceType == typeof(string)) || base.CanConvertFrom(context, sourceType));
- }
- public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
- {
- return ((destinationType == typeof(string)) || base.CanConvertTo(context, destinationType));
- }
- public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
- {
- if (value is string)
- {
- string[] strArray = (value as string).Split(new char[] { ',' });
- if (strArray.Length > 0)
- {
- TimePeriod[] periodArray = new TimePeriod[strArray.Length];
- TypeConverter converter = TypeDescriptor.GetConverter(typeof(TimePeriod));
- for (int i = 0; i < strArray.Length; i++)
- {
- periodArray[i] = (TimePeriod) converter.ConvertFromString(strArray[i]);
- }
- return periodArray;
- }
- }
- return base.ConvertFrom(context, culture, value);
- }
- public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
- {
- if ((destinationType == typeof(string)) && (value is TimePeriod[]))
- {
- string str = "";
- foreach (TimePeriod period in value as TimePeriod[])
- {
- if (str != "")
- {
- str = str + ",";
- }
- str = str + period.ToString();
- }
- return str;
- }
- return base.ConvertTo(context, culture, value, destinationType);
- }
- }
- }
|