EnumConverter.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. //
  12. // Permission is hereby granted, free of charge, to any person obtaining
  13. // a copy of this software and associated documentation files (the
  14. // "Software"), to deal in the Software without restriction, including
  15. // without limitation the rights to use, copy, modify, merge, publish,
  16. // distribute, sublicense, and/or sell copies of the Software, and to
  17. // permit persons to whom the Software is furnished to do so, subject to
  18. // the following conditions:
  19. //
  20. // The above copyright notice and this permission notice shall be
  21. // included in all copies or substantial portions of the Software.
  22. //
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  24. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  25. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  26. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  27. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  28. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  29. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  30. //
  31. using System;
  32. using System.Collections;
  33. using System.Globalization;
  34. namespace System.ComponentModel
  35. {
  36. public class EnumConverter : TypeConverter
  37. {
  38. private Type type;
  39. private StandardValuesCollection stdValues;
  40. public EnumConverter (Type type)
  41. {
  42. this.type = type;
  43. }
  44. public override bool CanConvertTo (ITypeDescriptorContext context, Type destinationType)
  45. {
  46. return base.CanConvertTo (context, destinationType);
  47. }
  48. public override object ConvertTo (ITypeDescriptorContext context,
  49. CultureInfo culture,
  50. object value,
  51. Type destinationType)
  52. {
  53. if (destinationType == typeof (string))
  54. if (value != null)
  55. return Enum.Format (type, value, "G");
  56. return base.ConvertTo (context, culture, value, destinationType);
  57. }
  58. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  59. {
  60. if (sourceType == typeof (string))
  61. return true;
  62. return base.CanConvertFrom (context, sourceType);
  63. }
  64. public override object ConvertFrom (ITypeDescriptorContext context,
  65. CultureInfo culture,
  66. object value)
  67. {
  68. string val = value as string;
  69. if (val == null)
  70. return base.ConvertFrom(context, culture, value);
  71. return Enum.Parse (type, val, true);
  72. }
  73. public override bool IsValid (ITypeDescriptorContext context, object value)
  74. {
  75. return Enum.IsDefined (type, value);
  76. }
  77. public override bool GetStandardValuesSupported (ITypeDescriptorContext context)
  78. {
  79. return true;
  80. }
  81. public override bool GetStandardValuesExclusive (ITypeDescriptorContext context)
  82. {
  83. return !(type.IsDefined (typeof (FlagsAttribute), false));
  84. }
  85. public override StandardValuesCollection GetStandardValues (ITypeDescriptorContext context)
  86. {
  87. if (stdValues == null) {
  88. Array values = Enum.GetValues (type);
  89. Array.Sort (values);
  90. stdValues = new StandardValuesCollection (values);
  91. }
  92. return stdValues;
  93. }
  94. protected virtual IComparer Comparer {
  95. get { return new EnumConverter.EnumComparer (); }
  96. }
  97. protected Type EnumType {
  98. get { return type; }
  99. }
  100. protected TypeConverter.StandardValuesCollection Values {
  101. get { return stdValues; }
  102. set { stdValues = value; }
  103. }
  104. private class EnumComparer : IComparer
  105. {
  106. int IComparer.Compare (object compareObject1, object compareObject2)
  107. {
  108. string CompareString1 = (compareObject1 as string);
  109. string CompareString2 = (compareObject2 as string);
  110. if ((CompareString1 == null) || (CompareString2 == null))
  111. return Collections.Comparer.Default.Compare (compareObject1, compareObject2);
  112. return CultureInfo.InvariantCulture.CompareInfo.Compare (CompareString1, CompareString2);
  113. }
  114. }
  115. }
  116. }