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

  1. //------------------------------------------------------------------------------
  2. // <copyright file="AmbientValueAttribute.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.    
  20.     using System;
  21.     using System.ComponentModel;
  22.     using System.Diagnostics;
  23.     using System.Globalization;
  24.     using System.Runtime.InteropServices;
  25.     using System.Runtime.Serialization.Formatters;
  26.     using System.Security.Permissions;
  27.    
  28.     /// <devdoc>
  29.     /// <para>Specifies the ambient value for a property. The ambient value is the value you
  30.     /// can set into a property to make it inherit its ambient.</para>
  31.     /// </devdoc>
  32.     [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")]
  33.     [AttributeUsage(AttributeTargets.All)]
  34.     public sealed class AmbientValueAttribute : Attribute
  35.     {
  36.        
  37.         private readonly object value;
  38.        
  39.         /// <devdoc>
  40.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> 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 AmbientValueAttribute(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("Ambient 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.AmbientValueAttribute'/> class using a Unicode
  61.         /// character.</para>
  62.         /// </devdoc>
  63.         public AmbientValueAttribute(char value)
  64.         {
  65.             this.value = value;
  66.         }
  67.         /// <devdoc>
  68.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using an 8-bit unsigned
  69.         /// integer.</para>
  70.         /// </devdoc>
  71.         public AmbientValueAttribute(byte value)
  72.         {
  73.             this.value = value;
  74.         }
  75.         /// <devdoc>
  76.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a 16-bit signed
  77.         /// integer.</para>
  78.         /// </devdoc>
  79.         public AmbientValueAttribute(short value)
  80.         {
  81.             this.value = value;
  82.         }
  83.         /// <devdoc>
  84.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a 32-bit signed
  85.         /// integer.</para>
  86.         /// </devdoc>
  87.         public AmbientValueAttribute(int value)
  88.         {
  89.             this.value = value;
  90.         }
  91.         /// <devdoc>
  92.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a 64-bit signed
  93.         /// integer.</para>
  94.         /// </devdoc>
  95.         public AmbientValueAttribute(long value)
  96.         {
  97.             this.value = value;
  98.         }
  99.         /// <devdoc>
  100.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a
  101.         /// single-precision floating point
  102.         /// number.</para>
  103.         /// </devdoc>
  104.         public AmbientValueAttribute(float value)
  105.         {
  106.             this.value = value;
  107.         }
  108.         /// <devdoc>
  109.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a
  110.         /// double-precision floating point
  111.         /// number.</para>
  112.         /// </devdoc>
  113.         public AmbientValueAttribute(double value)
  114.         {
  115.             this.value = value;
  116.         }
  117.         /// <devdoc>
  118.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a <see cref='System.Boolean'/>
  119.         /// value.</para>
  120.         /// </devdoc>
  121.         public AmbientValueAttribute(bool value)
  122.         {
  123.             this.value = value;
  124.         }
  125.         /// <devdoc>
  126.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/> class using a <see cref='System.String'/>.</para>
  127.         /// </devdoc>
  128.         public AmbientValueAttribute(string value)
  129.         {
  130.             this.value = value;
  131.         }
  132.        
  133.         /// <devdoc>
  134.         /// <para>Initializes a new instance of the <see cref='System.ComponentModel.AmbientValueAttribute'/>
  135.         /// class.</para>
  136.         /// </devdoc>
  137.         public AmbientValueAttribute(object value)
  138.         {
  139.             this.value = value;
  140.         }
  141.        
  142.         /// <devdoc>
  143.         /// <para>
  144.         /// Gets the ambient value of the property this
  145.         /// attribute is
  146.         /// bound to.
  147.         /// </para>
  148.         /// </devdoc>
  149.         public 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.             AmbientValueAttribute other = obj as AmbientValueAttribute;
  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. }

Developer Fusion