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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="CharConverter.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;
  21.     using System.Diagnostics;
  22.     using System.Globalization;
  23.     using System.Runtime.Serialization.Formatters;
  24.     using System.Runtime.Remoting;
  25.     using System.Runtime.InteropServices;
  26.     using System.Security.Permissions;
  27.    
  28.     /// <devdoc>
  29.     /// <para>Provides
  30.     /// a type converter to convert Unicode
  31.     /// character objects to and from various other representations.</para>
  32.     /// </devdoc>
  33.     [HostProtection(SharedState = true)]
  34.     public class CharConverter : TypeConverter
  35.     {
  36.        
  37.         /// <devdoc>
  38.         /// <para>Gets a value indicating whether this converter can
  39.         /// convert an object in the given source type to a Unicode character object using
  40.         /// the specified context.</para>
  41.         /// </devdoc>
  42.         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  43.         {
  44.             if (sourceType == typeof(string)) {
  45.                 return true;
  46.             }
  47.             return base.CanConvertFrom(context, sourceType);
  48.         }
  49.        
  50.         /// <devdoc>
  51.         /// Converts the given object to another type.
  52.         /// </devdoc>
  53.         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  54.         {
  55.             if (destinationType == typeof(string) && value is char) {
  56.                 if ((char)value == (char)0) {
  57.                     return "";
  58.                 }
  59.             }
  60.            
  61.             return base.ConvertTo(context, culture, value, destinationType);
  62.         }
  63.        
  64.         /// <devdoc>
  65.         /// <para>Converts the given object to a Unicode character object.</para>
  66.         /// </devdoc>
  67.         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  68.         {
  69.             if (value is string) {
  70.                 string text = (string)value;
  71.                 if (text.Length > 1) {
  72.                     text = text.Trim();
  73.                 }
  74.                
  75.                 if (text != null && text.Length > 0) {
  76.                     if (text.Length != 1) {
  77.                         throw new FormatException(SR.GetString(SR.ConvertInvalidPrimitive, text, "Char"));
  78.                     }
  79.                     return text[0];
  80.                 }
  81.                
  82.                 return '\0';
  83.             }
  84.             return base.ConvertFrom(context, culture, value);
  85.         }
  86.     }
  87. }

Developer Fusion