TimeoutConverter.cs 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //------------------------------------------------------------------------------
  2. // <copyright file="TimeoutConverter.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\TimeoutConverter.uex' path='docs/doc[@for="TimeoutConverter"]/*' />
  13. /// <devdoc>
  14. /// TimeoutConverter is a class that can be used to convert
  15. /// Timeout from one data type to another. Access this
  16. /// class through the TypeDescriptor.
  17. /// </devdoc>
  18. internal class TimeoutConverter : TypeConverter
  19. {
  20. /// <include file='doc\TimeoutConverter.uex' path='docs/doc[@for="TimeoutConverter.CanConvertFrom"]/*' />
  21. /// <devdoc>
  22. /// Determines if this converter can convert an object in the given source
  23. /// type to the native type of the converter.
  24. /// </devdoc>
  25. public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  26. {
  27. if (sourceType == typeof(string))
  28. {
  29. return true;
  30. }
  31. return base.CanConvertFrom(context, sourceType);
  32. }
  33. /// <include file='doc\TimeoutConverter.uex' path='docs/doc[@for="TimeoutConverter.ConvertFrom"]/*' />
  34. /// <devdoc>
  35. /// Converts the given object to the converter's native type.
  36. /// </devdoc>
  37. public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  38. {
  39. if (value is string)
  40. {
  41. string text = ((string)value).Trim();
  42. if (text.Length == 0 || (string.Compare(text, Res.GetString(Res.InfiniteValue), true, CultureInfo.CurrentCulture) == 0))
  43. return TimeSpan.FromSeconds((double)uint.MaxValue);
  44. else
  45. {
  46. double totalSeconds = Convert.ToDouble(text, culture);
  47. if (totalSeconds > (double)uint.MaxValue)
  48. totalSeconds = (double)uint.MaxValue;
  49. return TimeSpan.FromSeconds(totalSeconds);
  50. }
  51. }
  52. return base.ConvertFrom(context, culture, value);
  53. }
  54. /// <include file='doc\TimeoutConverter.uex' path='docs/doc[@for="TimeoutConverter.ConvertTo"]/*' />
  55. /// <devdoc>
  56. /// Converts the given object to another type. The most common types to convert
  57. /// are to and from a string object. The default implementation will make a call
  58. /// to ToString on the object if the object is valid and if the destination
  59. /// type is string. If this cannot convert to the desitnation type, this will
  60. /// throw a NotSupportedException.
  61. /// </devdoc>
  62. public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  63. {
  64. if (destinationType == null)
  65. {
  66. throw new ArgumentNullException("destinationType");
  67. }
  68. if (destinationType == typeof(string))
  69. {
  70. if (value != null)
  71. {
  72. double totalSeconds = ((TimeSpan)value).TotalSeconds;
  73. if (totalSeconds >= uint.MaxValue)
  74. return Res.GetString(Res.InfiniteValue);
  75. else
  76. return ((uint)totalSeconds).ToString(culture);
  77. }
  78. }
  79. return base.ConvertTo(context, culture, value, destinationType);
  80. }
  81. }
  82. }