FloatArrayConverter.cs 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. namespace IndexFormula.Finance
  2. {
  3. using System;
  4. using System.Collections;
  5. using System.ComponentModel;
  6. using System.Globalization;
  7. public class FloatArrayConverter : ArrayConverter
  8. {
  9. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  10. {
  11. return ((((sourceType == null) || (sourceType == typeof(string))) || (sourceType == typeof(float[]))) || base.CanConvertFrom(context, sourceType));
  12. }
  13. public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  14. {
  15. return ((((destinationType == null) || (destinationType == typeof(string))) || (destinationType == typeof(float[]))) || base.CanConvertTo(context, destinationType));
  16. }
  17. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  18. {
  19. if (value == null)
  20. {
  21. return null;
  22. }
  23. if (value is string)
  24. {
  25. string[] strArray = (value as string).Split(new char[] { ',' });
  26. if (strArray.Length == 0)
  27. {
  28. return null;
  29. }
  30. float[] numArray = new float[strArray.Length];
  31. for (int i = 0; i < numArray.Length; i++)
  32. {
  33. numArray[i] = float.Parse(strArray[i], NumberFormatInfo.InvariantInfo);
  34. }
  35. return numArray;
  36. }
  37. if (value is float[])
  38. {
  39. return ((float[]) value).Clone();
  40. }
  41. return base.ConvertFrom(context, culture, value);
  42. }
  43. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  44. {
  45. if ((destinationType == typeof(string)) && (value is float[]))
  46. {
  47. string str = "";
  48. foreach (float num in (float[]) value)
  49. {
  50. if (str.Length != 0)
  51. {
  52. str = str + ",";
  53. }
  54. str = str + num.ToString();
  55. }
  56. return str;
  57. }
  58. return base.ConvertTo(context, culture, value, destinationType);
  59. }
  60. public override object CreateInstance(ITypeDescriptorContext context, IDictionary propertyValues)
  61. {
  62. return base.CreateInstance(context, propertyValues);
  63. }
  64. public override bool GetCreateInstanceSupported(ITypeDescriptorContext context)
  65. {
  66. return true;
  67. }
  68. }
  69. }