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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="DefaultValueAttribute.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. namespace System.ComponentModel
  16. {
  17.    
  18.     using System;
  19.     using System.ComponentModel;
  20.     using System.Diagnostics;
  21.     using System.Globalization;
  22.     using System.Runtime.InteropServices;
  23.     using System.Runtime.Serialization.Formatters;
  24.     using System.Security.Permissions;
  25.    
  26.     /// <devdoc>
  27.     /// <para>Specifies the default value for a property.</para>
  28.     /// </devdoc>
  29.     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")]
  30.     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes")]
  31.     [AttributeUsage(AttributeTargets.All)]
  32.     public class DefaultValueAttribute : Attribute
  33.     {
  34.         /// <devdoc>
  35.         /// This is the default value.
  36.         /// </devdoc>
  37.         private object value;
  38.        
  39.         /// <devdoc>
  40.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class, converting the
  41.         /// specified value to the
  42.         /// specified type, and using the U.S. English culture as the
  43.         /// translation
  44.         /// context.</para>
  45.         /// </devdoc>
  46.         public DefaultValueAttribute(Type type, string value)
  47.         {
  48.            
  49.             // The try/catch here is because attributes should never throw exceptions. We would fail to
  50.             // load an otherwise normal class.
  51.             try {
  52.                 this.value = TypeDescriptor.GetConverter(type).ConvertFromInvariantString(value);
  53.             }
  54.             catch {
  55.                 Debug.Fail("Default value attribute of type " + type.FullName + " threw converting from the string '" + value + "'.");
  56.             }
  57.         }
  58.        
  59.         /// <devdoc>
  60.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a Unicode
  61.         /// character.</para>
  62.         /// </devdoc>
  63.         public DefaultValueAttribute(char value)
  64.         {
  65.             this.value = value;
  66.         }
  67.         /// <devdoc>
  68.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using an 8-bit unsigned
  69.         /// integer.</para>
  70.         /// </devdoc>
  71.         public DefaultValueAttribute(byte value)
  72.         {
  73.             this.value = value;
  74.         }
  75.         /// <devdoc>
  76.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a 16-bit signed
  77.         /// integer.</para>
  78.         /// </devdoc>
  79.         public DefaultValueAttribute(short value)
  80.         {
  81.             this.value = value;
  82.         }
  83.         /// <devdoc>
  84.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a 32-bit signed
  85.         /// integer.</para>
  86.         /// </devdoc>
  87.         public DefaultValueAttribute(int value)
  88.         {
  89.             this.value = value;
  90.         }
  91.         /// <devdoc>
  92.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a 64-bit signed
  93.         /// integer.</para>
  94.         /// </devdoc>
  95.         public DefaultValueAttribute(long value)
  96.         {
  97.             this.value = value;
  98.         }
  99.         /// <devdoc>
  100.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a
  101.         /// single-precision floating point
  102.         /// number.</para>
  103.         /// </devdoc>
  104.         public DefaultValueAttribute(float value)
  105.         {
  106.             this.value = value;
  107.         }
  108.         /// <devdoc>
  109.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a
  110.         /// double-precision floating point
  111.         /// number.</para>
  112.         /// </devdoc>
  113.         public DefaultValueAttribute(double value)
  114.         {
  115.             this.value = value;
  116.         }
  117.         /// <devdoc>
  118.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a <see cref='System.Boolean'/>
  119.         /// value.</para>
  120.         /// </devdoc>
  121.         public DefaultValueAttribute(bool value)
  122.         {
  123.             this.value = value;
  124.         }
  125.         /// <devdoc>
  126.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a <see cref='System.String'/>.</para>
  127.         /// </devdoc>
  128.         public DefaultValueAttribute(string value)
  129.         {
  130.             this.value = value;
  131.         }
  132.        
  133.         /// <devdoc>
  134.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/>
  135.         /// class.</para>
  136.         /// </devdoc>
  137.         public DefaultValueAttribute(object value)
  138.         {
  139.             this.value = value;
  140.         }
  141.        
  142.         /// <devdoc>
  143.         /// <para>
  144.         /// Gets the default value of the property this
  145.         /// attribute is
  146.         /// bound to.
  147.         /// </para>
  148.         /// </devdoc>
  149.         public virtual object Value {
  150.             get { return value; }
  151.         }
  152.        
  153.         public override bool Equals(object obj)
  154.         {
  155.             if (obj == this) {
  156.                 return true;
  157.             }
  158.            
  159.             DefaultValueAttribute other = obj as DefaultValueAttribute;
  160.            
  161.             if (other != null) {
  162.                 if (Value != null) {
  163.                     return Value.Equals(other.Value);
  164.                 }
  165.                 else {
  166.                     return (other.Value == null);
  167.                 }
  168.             }
  169.             return false;
  170.         }
  171.        
  172.         public override int GetHashCode()
  173.         {
  174.             return base.GetHashCode();
  175.         }
  176.        
  177.         protected void SetValue(object value)
  178.         {
  179.             this.value = value;
  180.         }
  181.     }
  182. }

Developer Fusion