EnumConverter.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. //
  2. // System.ComponentModel.EnumConverter.cs
  3. //
  4. // Authors:
  5. // Gonzalo Paniagua Javier ([email protected])
  6. // Andreas Nahr ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc (http://www.ximian.com)
  9. // (C) 2003 Andreas Nahr
  10. //
  11. using System;
  12. using System.Collections;
  13. using System.Globalization;
  14. namespace System.ComponentModel
  15. {
  16. public class EnumConverter : TypeConverter
  17. {
  18. private Type type;
  19. private StandardValuesCollection stdValues;
  20. public EnumConverter (Type type)
  21. {
  22. this.type = type;
  23. }
  24. [MonoTODO]
  25. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  26. {
  27. return base.CanConvertTo (context, destinationType);
  28. }
  29. [MonoTODO]
  30. public override object ConvertTo (ITypeDescriptorContext context,
  31. CultureInfo culture,
  32. object value,
  33. Type destinationType)
  34. {
  35. if (destinationType == typeof (string))
  36. if (value != null)
  37. return Enum.Format (type, value, "G");
  38. return base.ConvertTo (context, culture, value, destinationType);
  39. }
  40. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  41. {
  42. if (sourceType == typeof (string))
  43. return true;
  44. return base.CanConvertFrom (context, sourceType);
  45. }
  46. public override object ConvertFrom (ITypeDescriptorContext context,
  47. CultureInfo culture,
  48. object value)
  49. {
  50. string val = value as string;
  51. if (val == null)
  52. return base.ConvertFrom(context, culture, value);
  53. return Enum.Parse (type, val, true);
  54. }
  55. public override bool IsValid (ITypeDescriptorContext context, object value)
  56. {
  57. return Enum.IsDefined (type, value);
  58. }
  59. public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
  60. {
  61. return true;
  62. }
  63. public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
  64. {
  65. return !(type.IsDefined (typeof (FlagsAttribute), false));
  66. }
  67. public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
  68. {
  69. if (stdValues == null) {
  70. Array values = Enum.GetValues (type);
  71. Array.Sort (values);
  72. stdValues = new StandardValuesCollection (values);
  73. }
  74. return stdValues;
  75. }
  76. protected virtual IComparer Comparer {
  77. get { return new EnumConverter.EnumComparer (); }
  78. }
  79. protected Type EnumType {
  80. get { return type; }
  81. }
  82. protected TypeConverter.StandardValuesCollection Values {
  83. get { return stdValues; }
  84. set { stdValues = value; }
  85. }
  86. private class EnumComparer : IComparer
  87. {
  88. int IComparer.Compare (object compareObject1, object compareObject2)
  89. {
  90. string CompareString1 = (compareObject1 as string);
  91. string CompareString2 = (compareObject2 as string);
  92. if ((CompareString1 == null) || (CompareString2 == null))
  93. return Collections.Comparer.Default.Compare (compareObject1, compareObject2);
  94. return CultureInfo.InvariantCulture.CompareInfo.Compare (CompareString1, CompareString2);
  95. }
  96. }
  97. }
  98. }