DefaultValueAttribute.cs 8.4 KB

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