DefaultValueAttribute.cs 8.6 KB

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