* SingleConverter.cs: Implement conversion from string to match MS.NET.
[mono.git] / mcs / class / System / System.ComponentModel / DefaultValueAttribute.cs
1 //
2 // System.ComponentModel.DefaultValueAttribute.cs
3 //
4 // Authors:
5 //   Andreas Nahr (ClassDevelopment@A-SoftTech.com)
6 //
7 // (C) 2003 Andreas Nahr
8 //
9
10 //
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
18 // 
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
21 // 
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
29 //
30
31 using System.Globalization;
32 namespace System.ComponentModel
33 {
34         [AttributeUsage(AttributeTargets.All)]
35         public sealed class DefaultValueAttribute : Attribute
36         {
37
38                 private object DefaultValue;
39
40                 public DefaultValueAttribute (bool value)
41                 {
42                         DefaultValue = value;
43                 }
44
45                 public DefaultValueAttribute (byte value)
46                 {
47                         DefaultValue = value;
48                 }
49
50                 public DefaultValueAttribute (char value)
51                 {
52                         DefaultValue = value;
53                 }
54
55                 public DefaultValueAttribute (double value)
56                 {
57                         DefaultValue = value;
58                 }
59
60                 public DefaultValueAttribute (short value)
61                 {
62                         DefaultValue = value;
63                 }
64
65                 public DefaultValueAttribute (int value)
66                 {
67                         DefaultValue = value;
68                 }
69
70                 public DefaultValueAttribute (long value)
71                 {
72                         DefaultValue = value;
73                 }
74
75                 public DefaultValueAttribute (object value)
76                 {
77                         DefaultValue = value;
78                 }
79
80                 public DefaultValueAttribute (float value)
81                 {
82                         DefaultValue = value;
83                 }
84
85                 public DefaultValueAttribute (string value)
86                 {
87                         DefaultValue = value;
88                 }
89
90                 public DefaultValueAttribute (Type type, string value)
91                 {
92                         try {
93                                 TypeConverter converter = TypeDescriptor.GetConverter (type);
94                                 DefaultValue = converter.ConvertFromString (null, CultureInfo.InvariantCulture, value);
95                         } catch { }
96                 }
97
98                 public object Value {
99                         get { return DefaultValue; }
100                 }
101
102                 public override bool Equals (object obj)
103                 {
104                         if (!(obj is DefaultValueAttribute))
105                                 return false;
106                         if (obj == this)
107                                 return true;
108                         return ((DefaultValueAttribute) obj).Value == DefaultValue;
109                 }
110
111                 public override int GetHashCode()
112                 {
113                         return DefaultValue.GetHashCode();
114                 }
115         }
116 }