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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="BooleanConverter.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.Collections;
  21.     using System.ComponentModel;
  22.     using System.Diagnostics;
  23.     using System.Globalization;
  24.     using System.Runtime.InteropServices;
  25.     using System.Runtime.Remoting;
  26.     using System.Runtime.Serialization.Formatters;
  27.     using System.Security.Permissions;
  28.    
  29.     /// <devdoc>
  30.     /// <para>Provides a type converter to convert
  31.     /// Boolean objects to and from various other representations.</para>
  32.     /// </devdoc>
  33.     [HostProtection(SharedState = true)]
  34.     public class BooleanConverter : TypeConverter
  35.     {
  36.         private static StandardValuesCollection values;
  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 Boolean 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>Converts the given value
  54.         /// object to a Boolean object.</para>
  55.         /// </devdoc>
  56.         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  57.         {
  58.             if (value is string) {
  59.                 string text = ((string)value).Trim();
  60.                 try {
  61.                     return Boolean.Parse(text);
  62.                 }
  63.                 catch (FormatException e) {
  64.                     throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, (string)value, "Boolean"), e);
  65.                 }
  66.             }
  67.             return base.ConvertFrom(context, culture, value);
  68.         }
  69.        
  70.         /// <devdoc>
  71.         /// <para>Gets a collection of standard values
  72.         /// for the Boolean data type.</para>
  73.         /// </devdoc>
  74.         public override StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
  75.         {
  76.             if (values == null) {
  77.                 values = new StandardValuesCollection(new object[] {true, false});
  78.             }
  79.             return values;
  80.         }
  81.        
  82.         /// <devdoc>
  83.         /// <para>Gets a value indicating whether the list of standard values returned from
  84.         /// <see cref='System.ComponentModel.BooleanConverter.GetStandardValues'/> is an exclusive list. </para>
  85.         /// </devdoc>
  86.         public override bool GetStandardValuesExclusive(ITypeDescriptorContext context)
  87.         {
  88.             return true;
  89.         }
  90.        
  91.         /// <devdoc>
  92.         /// <para>Gets a value indicating whether this object supports a standard set of values
  93.         /// that can be picked from a list.</para>
  94.         /// </devdoc>
  95.         public override bool GetStandardValuesSupported(ITypeDescriptorContext context)
  96.         {
  97.             return true;
  98.         }
  99.     }
  100. }

Developer Fusion