The Labs \ Source Viewer \ SSCLI \ System.ComponentModel \ TimeSpanConverter

  1. //------------------------------------------------------------------------------
  2. // <copyright file="TimeSpanConverter.cs" company="Microsoft">
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. // </copyright>
  14. //------------------------------------------------------------------------------
  15. /*
  16. */
  17. namespace System.ComponentModel
  18. {
  19.     using Microsoft.Win32;
  20.     using System.ComponentModel.Design.Serialization;
  21.     using System.Diagnostics;
  22.     using System.Globalization;
  23.     using System.Reflection;
  24.     using System.Runtime.InteropServices;
  25.     using System.Runtime.Remoting;
  26.     using System.Runtime.Serialization.Formatters;
  27.     using System.Security.Permissions;
  28.     using System.Threading;
  29.    
  30.     /// <devdoc>
  31.     /// <para>Provides a type converter to convert <see cref='System.TimeSpan'/>
  32.     /// objects to and from various
  33.     /// other representations.</para>
  34.     /// </devdoc>
  35.     [HostProtection(SharedState = true)]
  36.     public class TimeSpanConverter : TypeConverter
  37.     {
  38.        
  39.         /// <devdoc>
  40.         /// <para>Gets a value indicating whether this converter can
  41.         /// convert an object in the given source type to a <see cref='System.TimeSpan'/> object using the
  42.         /// specified context.</para>
  43.         /// </devdoc>
  44.         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  45.         {
  46.             if (sourceType == typeof(string)) {
  47.                 return true;
  48.             }
  49.             return base.CanConvertFrom(context, sourceType);
  50.         }
  51.        
  52.         /// <devdoc>
  53.         /// <para>Gets a value indicating whether this converter can
  54.         /// convert an object to the given destination type using the context.</para>
  55.         /// </devdoc>
  56.         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  57.         {
  58.             if (destinationType == typeof(InstanceDescriptor)) {
  59.                 return true;
  60.             }
  61.             return base.CanConvertTo(context, destinationType);
  62.         }
  63.        
  64.         /// <devdoc>
  65.         /// <para>Converts the given object to a <see cref='System.TimeSpan'/>
  66.         /// object.</para>
  67.         /// </devdoc>
  68.         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  69.         {
  70.             if (value is string) {
  71.                 string text = ((string)value).Trim();
  72.                 try {
  73.                     return TimeSpan.Parse(text);
  74.                 }
  75.                 catch (FormatException e) {
  76.                     throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, (string)value, "TimeSpan"), e);
  77.                 }
  78.             }
  79.            
  80.             return base.ConvertFrom(context, culture, value);
  81.         }
  82.        
  83.         /// <devdoc>
  84.         /// Converts the given object to another type. The most common types to convert
  85.         /// are to and from a string object. The default implementation will make a call
  86.         /// to ToString on the object if the object is valid and if the destination
  87.         /// type is string. If this cannot convert to the desitnation type, this will
  88.         /// throw a NotSupportedException.
  89.         /// </devdoc>
  90.         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  91.         {
  92.             if (destinationType == null) {
  93.                 throw new ArgumentNullException("destinationType");
  94.             }
  95.            
  96.             if (destinationType == typeof(InstanceDescriptor) && value is TimeSpan) {
  97.                 MethodInfo method = typeof(TimeSpan).GetMethod("Parse", new Type[] {typeof(string)});
  98.                 if (method != null) {
  99.                     return new InstanceDescriptor(method, new object[] {value.ToString()});
  100.                 }
  101.             }
  102.            
  103.             return base.ConvertTo(context, culture, value, destinationType);
  104.         }
  105.     }
  106. }

Developer Fusion