|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.ComponentModel; |
| 7 | +using System.Diagnostics; |
| 8 | +using System.Globalization; |
| 9 | +using System.Runtime.InteropServices; |
| 10 | + |
| 11 | +namespace System.ComponentModel |
| 12 | +{ |
| 13 | + /// <devdoc> |
| 14 | + /// <para>Specifies the default value for a property.</para> |
| 15 | + /// </devdoc> |
| 16 | + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments")] |
| 17 | + [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1813:AvoidUnsealedAttributes")] |
| 18 | + [AttributeUsage(AttributeTargets.All)] |
| 19 | + public class DefaultValueAttribute : Attribute |
| 20 | + { |
| 21 | + /// <devdoc> |
| 22 | + /// This is the default value. |
| 23 | + /// </devdoc> |
| 24 | + private object _value; |
| 25 | + |
| 26 | + /// <devdoc> |
| 27 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class, converting the |
| 28 | + /// specified value to the |
| 29 | + /// specified type, and using the U.S. English culture as the |
| 30 | + /// translation |
| 31 | + /// context.</para> |
| 32 | + /// </devdoc> |
| 33 | + public DefaultValueAttribute(Type type, string value) |
| 34 | + { |
| 35 | + // The try/catch here is because attributes should never throw exceptions. We would fail to |
| 36 | + // load an otherwise normal class. |
| 37 | + try |
| 38 | + { |
| 39 | + if (type.IsSubclassOf(typeof(Enum))) |
| 40 | + { |
| 41 | + _value = Enum.Parse(type, value, true); |
| 42 | + } |
| 43 | + else if (type == typeof(TimeSpan)) |
| 44 | + { |
| 45 | + _value = TimeSpan.Parse(value); |
| 46 | + } |
| 47 | + else |
| 48 | + { |
| 49 | + _value = Convert.ChangeType(value, type, CultureInfo.InvariantCulture); |
| 50 | + } |
| 51 | + } |
| 52 | + catch |
| 53 | + { |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + /// <devdoc> |
| 58 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a Unicode |
| 59 | + /// character.</para> |
| 60 | + /// </devdoc> |
| 61 | + public DefaultValueAttribute(char value) |
| 62 | + { |
| 63 | + _value = value; |
| 64 | + } |
| 65 | + /// <devdoc> |
| 66 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using an 8-bit unsigned |
| 67 | + /// integer.</para> |
| 68 | + /// </devdoc> |
| 69 | + public DefaultValueAttribute(byte value) |
| 70 | + { |
| 71 | + _value = value; |
| 72 | + } |
| 73 | + /// <devdoc> |
| 74 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a 16-bit signed |
| 75 | + /// integer.</para> |
| 76 | + /// </devdoc> |
| 77 | + public DefaultValueAttribute(short value) |
| 78 | + { |
| 79 | + _value = value; |
| 80 | + } |
| 81 | + /// <devdoc> |
| 82 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a 32-bit signed |
| 83 | + /// integer.</para> |
| 84 | + /// </devdoc> |
| 85 | + public DefaultValueAttribute(int value) |
| 86 | + { |
| 87 | + _value = value; |
| 88 | + } |
| 89 | + /// <devdoc> |
| 90 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a 64-bit signed |
| 91 | + /// integer.</para> |
| 92 | + /// </devdoc> |
| 93 | + public DefaultValueAttribute(long value) |
| 94 | + { |
| 95 | + _value = value; |
| 96 | + } |
| 97 | + /// <devdoc> |
| 98 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a |
| 99 | + /// single-precision floating point |
| 100 | + /// number.</para> |
| 101 | + /// </devdoc> |
| 102 | + public DefaultValueAttribute(float value) |
| 103 | + { |
| 104 | + _value = value; |
| 105 | + } |
| 106 | + /// <devdoc> |
| 107 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a |
| 108 | + /// double-precision floating point |
| 109 | + /// number.</para> |
| 110 | + /// </devdoc> |
| 111 | + public DefaultValueAttribute(double value) |
| 112 | + { |
| 113 | + _value = value; |
| 114 | + } |
| 115 | + /// <devdoc> |
| 116 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a <see cref='System.Boolean'/> |
| 117 | + /// value.</para> |
| 118 | + /// </devdoc> |
| 119 | + public DefaultValueAttribute(bool value) |
| 120 | + { |
| 121 | + _value = value; |
| 122 | + } |
| 123 | + /// <devdoc> |
| 124 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a <see cref='System.String'/>.</para> |
| 125 | + /// </devdoc> |
| 126 | + public DefaultValueAttribute(string value) |
| 127 | + { |
| 128 | + _value = value; |
| 129 | + } |
| 130 | + |
| 131 | + /// <devdoc> |
| 132 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> |
| 133 | + /// class.</para> |
| 134 | + /// </devdoc> |
| 135 | + public DefaultValueAttribute(object value) |
| 136 | + { |
| 137 | + _value = value; |
| 138 | + } |
| 139 | + |
| 140 | + /// <devdoc> |
| 141 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a <see cref='System.SByte'/> |
| 142 | + /// value.</para> |
| 143 | + /// </devdoc> |
| 144 | + [CLSCompliant(false)] |
| 145 | + public DefaultValueAttribute(sbyte value) |
| 146 | + { |
| 147 | + _value = value; |
| 148 | + } |
| 149 | + |
| 150 | + /// <devdoc> |
| 151 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a <see cref='System.UInt16'/> |
| 152 | + /// value.</para> |
| 153 | + /// </devdoc> |
| 154 | + [CLSCompliant(false)] |
| 155 | + public DefaultValueAttribute(ushort value) |
| 156 | + { |
| 157 | + _value = value; |
| 158 | + } |
| 159 | + |
| 160 | + /// <devdoc> |
| 161 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a <see cref='System.UInt32'/> |
| 162 | + /// value.</para> |
| 163 | + /// </devdoc> |
| 164 | + [CLSCompliant(false)] |
| 165 | + public DefaultValueAttribute(uint value) |
| 166 | + { |
| 167 | + _value = value; |
| 168 | + } |
| 169 | + |
| 170 | + /// <devdoc> |
| 171 | + /// <para>Initializes a new instance of the <see cref='System.ComponentModel.DefaultValueAttribute'/> class using a <see cref='System.UInt64'/> |
| 172 | + /// value.</para> |
| 173 | + /// </devdoc> |
| 174 | + [CLSCompliant(false)] |
| 175 | + public DefaultValueAttribute(ulong value) |
| 176 | + { |
| 177 | + _value = value; |
| 178 | + } |
| 179 | + |
| 180 | + /// <devdoc> |
| 181 | + /// <para> |
| 182 | + /// Gets the default value of the property this |
| 183 | + /// attribute is |
| 184 | + /// bound to. |
| 185 | + /// </para> |
| 186 | + /// </devdoc> |
| 187 | + public virtual object Value |
| 188 | + { |
| 189 | + get |
| 190 | + { |
| 191 | + return _value; |
| 192 | + } |
| 193 | + } |
| 194 | + |
| 195 | + public override bool Equals(object obj) |
| 196 | + { |
| 197 | + if (obj == this) |
| 198 | + { |
| 199 | + return true; |
| 200 | + } |
| 201 | + |
| 202 | + DefaultValueAttribute other = obj as DefaultValueAttribute; |
| 203 | + |
| 204 | + if (other != null) |
| 205 | + { |
| 206 | + if (Value != null) |
| 207 | + { |
| 208 | + return Value.Equals(other.Value); |
| 209 | + } |
| 210 | + else |
| 211 | + { |
| 212 | + return (other.Value == null); |
| 213 | + } |
| 214 | + } |
| 215 | + return false; |
| 216 | + } |
| 217 | + |
| 218 | + public override int GetHashCode() |
| 219 | + { |
| 220 | + return base.GetHashCode(); |
| 221 | + } |
| 222 | + |
| 223 | + protected void SetValue(object value) |
| 224 | + { |
| 225 | + _value = value; |
| 226 | + } |
| 227 | + } |
| 228 | +} |
0 commit comments