DefaultValueAttribute.cs 8.8 KB

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