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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="DecimalConverter.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.    
  29.     /// <devdoc>
  30.     /// <para>Provides a type converter to convert <see cref='System.Decimal'/>
  31.     /// objects to and from various
  32.     /// other representations.</para>
  33.     /// </devdoc>
  34.     [HostProtection(SharedState = true)]
  35.     public class DecimalConverter : BaseNumberConverter
  36.     {
  37.        
  38.        
  39.         /// <devdoc>
  40.         /// Determines whether this editor will attempt to convert hex (0x or #) strings
  41.         /// </devdoc>
  42.         internal override bool AllowHex {
  43.             get { return false; }
  44.         }
  45.        
  46.         /// <devdoc>
  47.         /// The Type this converter is targeting (e.g. Int16, UInt32, etc.)
  48.         /// </devdoc>
  49.         internal override Type TargetType {
  50.             get { return typeof(decimal); }
  51.         }
  52.        
  53.         /// <devdoc>
  54.         /// <para>Gets a value indicating whether this converter can
  55.         /// convert an object to the given destination type using the context.</para>
  56.         /// </devdoc>
  57.         public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
  58.         {
  59.             if (destinationType == typeof(InstanceDescriptor)) {
  60.                 return true;
  61.             }
  62.             return base.CanConvertTo(context, destinationType);
  63.         }
  64.        
  65.         /// <devdoc>
  66.         /// Converts the given object to another type. The most common types to convert
  67.         /// are to and from a string object. The default implementation will make a call
  68.         /// to ToString on the object if the object is valid and if the destination
  69.         /// type is string. If this cannot convert to the desitnation type, this will
  70.         /// throw a NotSupportedException.
  71.         /// </devdoc>
  72.         public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)
  73.         {
  74.             if (destinationType == null) {
  75.                 throw new ArgumentNullException("destinationType");
  76.             }
  77.            
  78.             if (destinationType == typeof(InstanceDescriptor) && value is decimal) {
  79.                
  80.                 object[] args = new object[] {Decimal.GetBits((decimal)value)};
  81.                 MemberInfo member = typeof(decimal).GetConstructor(new Type[] {typeof(Int32[])});
  82.                
  83.                 Debug.Assert(member != null, "Could not convert decimal to member. Did someone change method name / signature and not update DecimalConverter?");
  84.                 if (member != null) {
  85.                     return new InstanceDescriptor(member, args);
  86.                 }
  87.                 else {
  88.                     return null;
  89.                 }
  90.             }
  91.            
  92.             return base.ConvertTo(context, culture, value, destinationType);
  93.         }
  94.        
  95.         /// <devdoc>
  96.         /// Convert the given value to a string using the given radix
  97.         /// </devdoc>
  98.         internal override object FromString(string value, int radix)
  99.         {
  100.             return Convert.ToDecimal(value, CultureInfo.CurrentCulture);
  101.         }
  102.        
  103.         /// <devdoc>
  104.         /// Convert the given value to a string using the given formatInfo
  105.         /// </devdoc>
  106.         internal override object FromString(string value, NumberFormatInfo formatInfo)
  107.         {
  108.             return Decimal.Parse(value, NumberStyles.Float, formatInfo);
  109.         }
  110.        
  111.        
  112.         /// <devdoc>
  113.         /// Convert the given value to a string using the given CultureInfo
  114.         /// </devdoc>
  115.         internal override object FromString(string value, CultureInfo culture)
  116.         {
  117.             return Decimal.Parse(value, culture);
  118.         }
  119.        
  120.         /// <devdoc>
  121.         /// Convert the given value from a string using the given formatInfo
  122.         /// </devdoc>
  123.         internal override string ToString(object value, NumberFormatInfo formatInfo)
  124.         {
  125.             return ((decimal)value).ToString("G", formatInfo);
  126.         }
  127.     }
  128. }

Developer Fusion