DefaultValueAttribute.cs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. //
  2. // System.ComponentModel.DefaultValueAttribute
  3. //
  4. // Authors:
  5. // Andreas Nahr ([email protected])
  6. //
  7. // (C) 2003 Andreas Nahr
  8. //
  9. namespace System.ComponentModel
  10. {
  11. [AttributeUsage(AttributeTargets.All)]
  12. public sealed class DefaultValueAttribute : Attribute
  13. {
  14. private object DefaultValue;
  15. public DefaultValueAttribute (bool value)
  16. {
  17. DefaultValue = value;
  18. }
  19. public DefaultValueAttribute (byte value)
  20. {
  21. DefaultValue = value;
  22. }
  23. public DefaultValueAttribute (char value)
  24. {
  25. DefaultValue = value;
  26. }
  27. public DefaultValueAttribute (double value)
  28. {
  29. DefaultValue = value;
  30. }
  31. public DefaultValueAttribute (short value)
  32. {
  33. DefaultValue = value;
  34. }
  35. public DefaultValueAttribute (int value)
  36. {
  37. DefaultValue = value;
  38. }
  39. public DefaultValueAttribute (long value)
  40. {
  41. DefaultValue = value;
  42. }
  43. public DefaultValueAttribute (object value)
  44. {
  45. DefaultValue = value;
  46. }
  47. public DefaultValueAttribute (float value)
  48. {
  49. DefaultValue = value;
  50. }
  51. public DefaultValueAttribute (string value)
  52. {
  53. DefaultValue = value;
  54. }
  55. public DefaultValueAttribute (Type type, string value)
  56. {
  57. // TODO check if this implementation is correct
  58. try
  59. {
  60. DefaultValue = Convert.ChangeType (value, type);
  61. }
  62. catch
  63. {
  64. DefaultValue = null;
  65. }
  66. }
  67. public object Value
  68. {
  69. get { return DefaultValue; }
  70. }
  71. public override bool Equals (object obj)
  72. {
  73. if (!(obj is DefaultValueAttribute))
  74. return false;
  75. if (obj == this)
  76. return true;
  77. return ((DefaultValueAttribute) obj).Value == DefaultValue;
  78. }
  79. public override int GetHashCode()
  80. {
  81. return DefaultValue.GetHashCode();
  82. }
  83. }
  84. }
  85. // OLD IMPLEMENTATION
  86. //using System;
  87. //
  88. //
  89. //
  90. //namespace System.ComponentModel
  91. //
  92. //{
  93. //
  94. // /// <summary>
  95. //
  96. // /// Specifies the default value for a property.
  97. //
  98. // /// </summary>
  99. //
  100. //
  101. //
  102. // [MonoTODO("Needs testing. DefaultValueAttribute(System.Type type, string value) is not implemented. Value has no description.")]
  103. //
  104. // [AttributeUsage(AttributeTargets.All)]
  105. //
  106. // public sealed class DefaultValueAttribute : Attribute
  107. //
  108. // {
  109. //
  110. //
  111. //
  112. // private object defaultValue;
  113. //
  114. //
  115. //
  116. // /// <summary>
  117. //
  118. // /// FIXME: Summary description for Value.
  119. //
  120. // /// </summary>
  121. //
  122. // public object Value
  123. //
  124. // {
  125. //
  126. // get
  127. //
  128. // {
  129. //
  130. // return defaultValue;
  131. //
  132. // }
  133. //
  134. // }
  135. //
  136. //
  137. //
  138. // /// <summary>
  139. //
  140. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class.
  141. //
  142. // /// </summary>
  143. //
  144. // /// <param name="value">An System.Object that represents the default value.</param>
  145. //
  146. // public DefaultValueAttribute(object value)
  147. //
  148. // {
  149. //
  150. // defaultValue = value;
  151. //
  152. // }
  153. //
  154. //
  155. //
  156. // /// <summary>
  157. //
  158. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using a System.Boolean value.
  159. //
  160. // /// </summary>
  161. //
  162. // /// <param name="value">An System.Boolean that represents the default value.</param>
  163. //
  164. // public DefaultValueAttribute(bool value)
  165. //
  166. // {
  167. //
  168. // defaultValue = value;
  169. //
  170. // }
  171. //
  172. //
  173. //
  174. //
  175. //
  176. // /// <summary>
  177. //
  178. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using an 8-bit unsigned integer.
  179. //
  180. // /// </summary>
  181. //
  182. // /// <param name="value">An 8-bit unsigned integer that is the default value.</param>
  183. //
  184. // public DefaultValueAttribute(byte value)
  185. //
  186. // {
  187. //
  188. // defaultValue = value;
  189. //
  190. // }
  191. //
  192. //
  193. //
  194. // /// <summary>
  195. //
  196. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using a Unicode character.
  197. //
  198. // /// </summary>
  199. //
  200. // /// <param name="value">A Unicode character that is the default value.</param>
  201. //
  202. // public DefaultValueAttribute(char value)
  203. //
  204. // {
  205. //
  206. // defaultValue = value;
  207. //
  208. // }
  209. //
  210. //
  211. //
  212. // /// <summary>
  213. //
  214. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using a double-precision floating point number.
  215. //
  216. // /// </summary>
  217. //
  218. // /// <param name="value">A double-precision floating point number that is the default value.</param>
  219. //
  220. // public DefaultValueAttribute(double value)
  221. //
  222. // {
  223. //
  224. // defaultValue = value;
  225. //
  226. // }
  227. //
  228. //
  229. //
  230. // /// <summary>
  231. //
  232. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using a 32-bit signed integer.
  233. //
  234. // /// </summary>
  235. //
  236. // /// <param name="value">A 32-bit signed integer that is the default value.</param>
  237. //
  238. // public DefaultValueAttribute(int value)
  239. //
  240. // {
  241. //
  242. // defaultValue = value;
  243. //
  244. // }
  245. //
  246. //
  247. //
  248. // /// <summary>
  249. //
  250. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using a 64-bit signed integer.
  251. //
  252. // /// </summary>
  253. //
  254. // /// <param name="value">A 64-bit signed integer that is the default value.</param>
  255. //
  256. // public DefaultValueAttribute(long value)
  257. //
  258. // {
  259. //
  260. // defaultValue = value;
  261. //
  262. // }
  263. //
  264. //
  265. //
  266. // /// <summary>
  267. //
  268. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using a 16-bit signed integer.
  269. //
  270. // /// </summary>
  271. //
  272. // /// <param name="value">A 16-bit signed integer that is the default value.</param>
  273. //
  274. // public DefaultValueAttribute(short value)
  275. //
  276. // {
  277. //
  278. // defaultValue = value;
  279. //
  280. // }
  281. //
  282. //
  283. //
  284. // /// <summary>
  285. //
  286. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using a single-precision floating point number.
  287. //
  288. // /// </summary>
  289. //
  290. // /// <param name="value">A single-precision floating point number that is the default value.</param>
  291. //
  292. // public DefaultValueAttribute(System.Single value)
  293. //
  294. // {
  295. //
  296. // defaultValue = value;
  297. //
  298. // }
  299. //
  300. //
  301. //
  302. // /// <summary>
  303. //
  304. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class using a System.String.
  305. //
  306. // /// </summary>
  307. //
  308. // /// <param name="value">A System.String that is the default value.</param>
  309. //
  310. // public DefaultValueAttribute(string value)
  311. //
  312. // {
  313. //
  314. // defaultValue = value;
  315. //
  316. // }
  317. //
  318. //
  319. //
  320. // /*
  321. //
  322. // /// <summary>
  323. //
  324. // /// Initializes a new instance of the System.ComponentModel.DefaultValueAttribute class, converting the specified value to the specified type, and using an invariant culture as the translation context.
  325. //
  326. // /// </summary>
  327. //
  328. // /// <param name="type">A System.Type that represents the type to convert the value to.</param>
  329. //
  330. // /// <param name="value">A System.String that can be converted to the type using the System.ComponentModel.TypeConverter for the type and the U.S. English culture.</param>
  331. //
  332. // public DefaultValueAttribute(System.Type type, string value)
  333. //
  334. // {
  335. //
  336. // //FIXME
  337. //
  338. // throw new NotImplementedException();
  339. //
  340. // }
  341. //
  342. // */
  343. //
  344. // }
  345. //
  346. //
  347. //
  348. //}