CharConverter.cs 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. //
  2. // System.ComponentModel.CharConverter
  3. //
  4. // Author:
  5. // Andreas Nahr ([email protected])
  6. //
  7. // (C) 2003 Andreas Nahr
  8. //
  9. using System.Globalization;
  10. namespace System.ComponentModel
  11. {
  12. public class CharConverter : TypeConverter
  13. {
  14. public override bool CanConvertFrom (ITypeDescriptorContext context, Type sourceType)
  15. {
  16. if (sourceType == typeof (string))
  17. return true;
  18. return base.CanConvertFrom (context, sourceType);
  19. }
  20. public override object ConvertFrom (ITypeDescriptorContext context, CultureInfo culture, object value)
  21. {
  22. if (value.GetType() == typeof (string)) {
  23. string Test = (String) value;
  24. if (Test.Length != 1)
  25. // LAMESPEC: MS does throw FormatException here
  26. throw new FormatException ("String has to be exactly one char long");
  27. return Test[0];
  28. }
  29. return base.ConvertFrom (context, culture, value);
  30. }
  31. public override object ConvertTo (ITypeDescriptorContext context,
  32. CultureInfo culture, object value, Type destinationType)
  33. {
  34. if (destinationType == typeof (string))
  35. if (value != null && value is char)
  36. return new string ((char) value, 1);
  37. return base.ConvertTo (context, culture, value, destinationType);
  38. }
  39. }
  40. }