SizeConverter.cs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. //------------------------------------------------------------------------------
  2. // <copyright file="SizeConverter.cs" company="Microsoft">
  3. // Copyright (c) Microsoft Corporation. All rights reserved.
  4. // </copyright>
  5. //------------------------------------------------------------------------------
  6. using Experimental.System.Messaging;
  7. using System;
  8. using System.ComponentModel;
  9. using System.Globalization;
  10. namespace Experimental.System.Messaging.Design
  11. {
  12. /// <include file='doc\SizeConverter.uex' path='docs/doc[@for="SizeConverter"]/*' />
  13. internal class SizeConverter : TypeConverter
  14. {
  15. /// <include file='doc\SizeConverter.uex' path='docs/doc[@for="SizeConverter.SizeConverter.CanConvertFrom"]/*' />
  16. /// <devdoc>
  17. /// Determines if this converter can convert an object in the given source
  18. /// type to the native type of the converter.
  19. /// </devdoc>
  20. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  21. {
  22. if (sourceType == typeof(string))
  23. {
  24. return true;
  25. }
  26. return base.CanConvertFrom(context, sourceType);
  27. }
  28. /// <include file='doc\SizeConverter.uex' path='docs/doc[@for="SizeConverter.SizeConverter.ConvertFrom"]/*' />
  29. /// <devdoc>
  30. /// Converts the given object to the converter's native type.
  31. /// </devdoc>
  32. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  33. {
  34. if (value is string)
  35. {
  36. string text = ((string)value).Trim();
  37. if (text.Length == 0 || string.Compare(text, Res.GetString(Res.InfiniteValue), true, CultureInfo.CurrentCulture) == 0)
  38. return (long)uint.MaxValue;
  39. else
  40. {
  41. long size = Convert.ToInt64(text, culture);
  42. return size;
  43. }
  44. }
  45. return base.ConvertFrom(context, culture, value);
  46. }
  47. /// <include file='doc\SizeConverter.uex' path='docs/doc[@for="SizeConverter.SizeConverter.ConvertTo"]/*' />
  48. /// <devdoc>
  49. /// Converts the given object to another type. The most common types to convert
  50. /// are to and from a string object. The default implementation will make a call
  51. /// to ToString on the object if the object is valid and if the destination
  52. /// type is string. If this cannot convert to the desitnation type, this will
  53. /// throw a NotSupportedException.
  54. /// </devdoc>
  55. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  56. {
  57. if (destinationType == null)
  58. {
  59. throw new ArgumentNullException("destinationType");
  60. }
  61. if (destinationType == typeof(string))
  62. {
  63. if (value != null)
  64. {
  65. if ((long)value == uint.MaxValue)
  66. return Res.GetString(Res.InfiniteValue);
  67. else
  68. return value.ToString();
  69. }
  70. }
  71. return base.ConvertTo(context, culture, value, destinationType);
  72. }
  73. }
  74. }