ec3738812310c8e3e9649b5a33d41af3353d9dd4
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationProperty.cs
1 //
2 // System.Configuration.ConfigurationProperty.cs
3 //
4 // Authors:
5 //      Duncan Mak (duncan@ximian.com)
6 //  Lluis Sanchez Gual (lluis@novell.com)
7 //
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
15 // 
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
18 // 
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 //
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
28 //
29
30 using System;
31 using System.ComponentModel;
32
33 namespace System.Configuration
34 {
35         public sealed class ConfigurationProperty
36         {
37                 internal static readonly object NoDefaultValue = new object ();
38                 
39                 string name;
40                 Type type;
41                 object default_value;
42                 TypeConverter converter;
43                 ConfigurationValidatorBase validation;
44                 ConfigurationPropertyOptions flags;
45                 string description;
46                 ConfigurationCollectionAttribute collectionAttribute;
47                 
48                 public ConfigurationProperty (string name, Type type)
49                         : this (name, type, NoDefaultValue, TypeDescriptor.GetConverter (type), new DefaultValidator(), ConfigurationPropertyOptions.None, null)
50                 { }
51
52                 public ConfigurationProperty (string name, Type type, object default_value)
53                         : this (name, type, default_value, TypeDescriptor.GetConverter (type), new DefaultValidator(), ConfigurationPropertyOptions.None, null)
54                 { }
55
56                 public ConfigurationProperty (
57                                         string name, Type type, object default_value,
58                                         ConfigurationPropertyOptions flags)
59                         :this (name, type, default_value, TypeDescriptor.GetConverter (type), new DefaultValidator(), flags, null)
60                 { }
61                 
62                 public ConfigurationProperty (
63                                         string name, Type type, object default_value,
64                                         TypeConverter converter,
65                                         ConfigurationValidatorBase validation,
66                                         ConfigurationPropertyOptions flags)
67                         : this (name, type, default_value, converter, validation, flags, null)
68                 { }
69
70                 public ConfigurationProperty (
71                                         string name, Type type, object default_value,
72                                         TypeConverter converter,
73                                         ConfigurationValidatorBase validation,
74                                         ConfigurationPropertyOptions flags,
75                                         string description)
76                 {
77                         this.name = name;
78                         this.converter = converter != null ? converter : TypeDescriptor.GetConverter (type);
79                         if (default_value != null) {
80                                 if (default_value == NoDefaultValue) {
81                                         switch (Type.GetTypeCode (type)) {
82                                         case TypeCode.Object:
83                                                 default_value = null;
84                                                 break;
85                                         case TypeCode.String:
86                                                 default_value = String.Empty;
87                                                 break;
88                                         default:
89                                                 default_value = Activator.CreateInstance (type);
90                                                 break;
91                                         }
92                                 }
93                                 else
94                                         if (!type.IsAssignableFrom (default_value.GetType ())) {
95                                                 if (!this.converter.CanConvertFrom (default_value.GetType ()))
96                                                         throw new ConfigurationErrorsException (String.Format ("The default value for property '{0}' has a different type than the one of the property itself: expected {1} but was {2}",
97                                                                                                            name, type, default_value.GetType ()));
98
99                                                 default_value = this.converter.ConvertFrom (default_value);
100                                         }
101                         }
102                         this.default_value = default_value;
103                         this.flags = flags;
104                         this.type = type;
105                         this.validation = validation != null ? validation : new DefaultValidator ();
106                         this.description = description;
107                 }
108
109                 public TypeConverter Converter {
110                         get { return converter; }
111                 }
112
113                 public object DefaultValue {                        
114                         get { return default_value; }
115                 }
116
117                 public bool IsKey {                        
118                         get { return (flags & ConfigurationPropertyOptions.IsKey) != 0; }
119                 }
120
121                 public bool IsRequired {
122                         get { return (flags & ConfigurationPropertyOptions.IsRequired) != 0; }
123                 }
124
125                 public bool IsDefaultCollection {
126                         get { return (flags & ConfigurationPropertyOptions.IsDefaultCollection) != 0; }
127                 }
128
129                 public string Name {
130                         get { return name; }
131                 }
132
133                 public string Description {
134                         get { return description; }
135                 }
136
137                 public Type Type {
138                         get { return type; }
139                 }
140
141                 public ConfigurationValidatorBase Validator {
142                         get { return validation; }
143                 }
144
145                 internal object ConvertFromString (string value)
146                 {
147                         if (converter != null)
148                                 return converter.ConvertFromInvariantString (value);
149                         else
150                                 throw new NotImplementedException ();
151                 }
152
153                 internal string ConvertToString (object value)
154                 {
155                         if (converter != null)
156                                 return converter.ConvertToInvariantString (value);
157                         else
158                                 throw new NotImplementedException ();
159                 }
160                 
161                 internal bool IsElement {
162                         get {
163                                 return (typeof(ConfigurationElement).IsAssignableFrom (type));
164                         }
165                 }
166                 
167                 internal ConfigurationCollectionAttribute CollectionAttribute {
168                         get { return collectionAttribute; }
169                         set { collectionAttribute = value; }
170                 }
171
172                 internal void Validate (object value)
173                 {
174                         if (validation != null)
175                                 validation.Validate (value);
176                 }
177         }
178 }
179