The Labs \ Source Viewer \ SSCLI \ System.Configuration \ InfiniteTimeSpanConverter

  1. //------------------------------------------------------------------------------
  2. // <copyright file="InfiniteTimeSpanConverter.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. using System;
  16. using System.Collections;
  17. using System.IO;
  18. using System.Reflection;
  19. using System.Security.Permissions;
  20. using System.Xml;
  21. using System.Collections.Specialized;
  22. using System.Globalization;
  23. using System.ComponentModel;
  24. using System.Security;
  25. using System.Text;
  26. namespace System.Configuration
  27. {
  28.    
  29.     public sealed class InfiniteTimeSpanConverter : ConfigurationConverterBase
  30.     {
  31.         static readonly TypeConverter s_TimeSpanConverter = TypeDescriptor.GetConverter(typeof(TimeSpan));
  32.        
  33.         public override object ConvertTo(ITypeDescriptorContext ctx, CultureInfo ci, object value, Type type)
  34.         {
  35.             ValidateType(value, typeof(TimeSpan));
  36.            
  37.             if ((TimeSpan)value == TimeSpan.MaxValue) {
  38.                 return "Infinite";
  39.             }
  40.             else {
  41.                 return s_TimeSpanConverter.ConvertToInvariantString(value);
  42.             }
  43.         }
  44.        
  45.         public override object ConvertFrom(ITypeDescriptorContext ctx, CultureInfo ci, object data)
  46.         {
  47.             Debug.Assert(data is string, "data is string");
  48.            
  49.             if ((string)data == "Infinite") {
  50.                 return TimeSpan.MaxValue;
  51.             }
  52.             else {
  53.                 return s_TimeSpanConverter.ConvertFromInvariantString((string)data);
  54.             }
  55.         }
  56.     }
  57. }

Developer Fusion