UnitConverter.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /**
  2. * Namespace: System.Web.UI.WebControls
  3. * Class: UnitConverter
  4. *
  5. * Author: Gaurav Vaish
  6. * Maintainer: [email protected]
  7. * Contact: <[email protected]>, <[email protected]>
  8. * Implementation: yes
  9. * Status: 100%
  10. *
  11. * (C) Gaurav Vaish (2002)
  12. */
  13. using System;
  14. using System.Globalization;
  15. using System.ComponentModel;
  16. using System.Web;
  17. using System.Web.UI;
  18. namespace System.Web.UI.WebControls
  19. {
  20. public class UnitConverter : TypeConverter
  21. {
  22. public UnitConverter(): base()
  23. {
  24. }
  25. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  26. {
  27. if(sourceType == typeof(string))
  28. return true;
  29. return CanConvertFrom(context, sourceType);
  30. }
  31. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  32. {
  33. if(value == null)
  34. return null;
  35. if(value is string)
  36. {
  37. string val = ((string)value).Trim();
  38. if(val.Length == 0)
  39. {
  40. return Unit.Empty;
  41. }
  42. return (culture == null ? Unit.Parse(val) : Unit.Parse(val, culture));
  43. }
  44. return ConvertFrom(context, culture, value);
  45. }
  46. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  47. {
  48. if(destinationType == typeof(string))
  49. {
  50. Unit val = (Unit)value;
  51. if(val == Unit.Empty)
  52. {
  53. return String.Empty;
  54. }
  55. return val.ToString(culture);
  56. }
  57. return ConvertTo(context, culture, value, destinationType);
  58. }
  59. }
  60. }