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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="basenumberconverter.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.Diagnostics;
  21.     using System.Globalization;
  22.     using System.Runtime.InteropServices;
  23.     using System.Runtime.Remoting;
  24.     using System.Runtime.Serialization.Formatters;
  25.     using System.Security.Permissions;
  26.    
  27.     /// <devdoc>
  28.     /// <para>Provides a base type converter for integral types.</para>
  29.     /// </devdoc>
  30.     [HostProtection(SharedState = true)]
  31.     public abstract class BaseNumberConverter : TypeConverter
  32.     {
  33.        
  34.        
  35.         /// <devdoc>
  36.         /// Determines whether this editor will attempt to convert hex (0x or #) strings
  37.         /// </devdoc>
  38.         internal virtual bool AllowHex {
  39.             get { return true; }
  40.         }
  41.        
  42.        
  43.         /// <devdoc>
  44.         /// The Type this converter is targeting (e.g. Int16, UInt32, etc.)
  45.         /// </devdoc>
  46.         internal abstract Type TargetType {
  47.             get;
  48.         }
  49.        
  50.         /// <devdoc>
  51.         /// Convert the given value to a string using the given radix
  52.         /// </devdoc>
  53.         internal abstract object FromString(string value, int radix);
  54.        
  55.         /// <devdoc>
  56.         /// Convert the given value to a string using the given formatInfo
  57.         /// </devdoc>
  58.         internal abstract object FromString(string value, NumberFormatInfo formatInfo);
  59.        
  60.         /// <devdoc>
  61.         /// Convert the given value to a string using the given CultureInfo
  62.         /// </devdoc>
  63.         internal abstract object FromString(string value, CultureInfo culture);
  64.        
  65.         /// <devdoc>
  66.         /// Create an error based on the failed text and the exception thrown.
  67.         /// </devdoc>
  68.         internal virtual Exception FromStringError(string failedText, Exception innerException)
  69.         {
  70.             return new Exception(SR.GetString(SR.ConvertInvalidPrimitive, failedText, TargetType.Name), innerException);
  71.         }
  72.        
  73.         /// <devdoc>
  74.         /// Convert the given value from a string using the given formatInfo
  75.         /// </devdoc>
  76.         internal abstract string ToString(object value, NumberFormatInfo formatInfo);
  77.        
  78.         /// <devdoc>
  79.         /// <para>Gets a value indicating whether this converter can convert an object in the
  80.         /// given source type to a 64-bit signed integer object using the specified context.</para>
  81.         /// </devdoc>
  82.         public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
  83.         {
  84.             if (sourceType == typeof(string)) {
  85.                 return true;
  86.             }
  87.             return base.CanConvertFrom(context, sourceType);
  88.         }
  89.        
  90.         /// <devdoc>
  91.         /// <para>Converts the given value object to a 64-bit signed integer object.</para>
  92.         /// </devdoc>
  93.         public override object ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, object value)
  94.         {
  95.             if (value is string) {
  96.                 string text = ((string)value).Trim();
  97.                
  98.                 try {
  99.                     if (AllowHex && text[0] == '#') {
  100.                         return FromString(text.Substring(1), 16);
  101.                     }
  102.                     else if (AllowHex && text.StartsWith("0x") || text.StartsWith("0X") || text.StartsWith("&h") || text.StartsWith("&H")) {
  103.                         return FromString(text.Substring(2), 16);
  104.                     }
  105.                     else {
  106.                         if (culture == null) {
  107.                             culture = CultureInfo.CurrentCulture;
  108.                         }
  109.                         NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
  110.                         return FromString(text, formatInfo);
  111.                     }
  112.                 }
  113.                 catch (Exception e) {
  114.                     throw FromStringError(text, e);
  115.                 }
  116.                 catch {
  117.                     throw;
  118.                 }
  119.             }
  120.             return base.ConvertFrom(context, culture, value);
  121.         }
  122.        
  123.         /// <devdoc>
  124.         /// <para>Converts the given value object to a 64-bit signed integer object using the
  125.         /// arguments.</para>
  126.         /// </devdoc>
  127.         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  128.         {
  129.             if (destinationType == null) {
  130.                 throw new ArgumentNullException("destinationType");
  131.             }
  132.            
  133.             if (destinationType == typeof(string) && value != null && TargetType.IsInstanceOfType(value)) {
  134.                
  135.                 if (culture == null) {
  136.                     culture = CultureInfo.CurrentCulture;
  137.                 }
  138.                 NumberFormatInfo formatInfo = (NumberFormatInfo)culture.GetFormat(typeof(NumberFormatInfo));
  139.                 return ToString(value, formatInfo);
  140.             }
  141.            
  142.             if (destinationType.IsPrimitive) {
  143.                 return Convert.ChangeType(value, destinationType, culture);
  144.             }
  145.             return base.ConvertTo(context, culture, value, destinationType);
  146.         }
  147.        
  148.         public override bool CanConvertTo(ITypeDescriptorContext context, Type t)
  149.         {
  150.             if (base.CanConvertTo(context, t) || t.IsPrimitive) {
  151.                 return true;
  152.             }
  153.             return false;
  154.         }
  155.     }
  156. }

Developer Fusion