[xbuild] Actually delete common files (CopyLocal) during Clean.
[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 #if NET_2_0
31 using System;
32 using System.ComponentModel;
33
34 namespace System.Configuration
35 {
36         public sealed class ConfigurationProperty
37         {
38                 internal static readonly object NoDefaultValue = new object ();
39                 
40                 string name;
41                 Type type;
42                 object default_value;
43                 TypeConverter converter;
44                 ConfigurationValidatorBase validation;
45                 ConfigurationPropertyOptions flags;
46                 string description;
47                 ConfigurationCollectionAttribute collectionAttribute;
48                 
49                 public ConfigurationProperty (string name, Type type)
50                         : this (name, type, NoDefaultValue, TypeDescriptor.GetConverter (type), new DefaultValidator(), ConfigurationPropertyOptions.None, null)
51                 { }
52
53                 public ConfigurationProperty (string name, Type type, object default_value)
54                         : this (name, type, default_value, TypeDescriptor.GetConverter (type), new DefaultValidator(), ConfigurationPropertyOptions.None, null)
55                 { }
56
57                 public ConfigurationProperty (
58                                         string name, Type type, object default_value,
59                                         ConfigurationPropertyOptions flags)
60                         :this (name, type, default_value, TypeDescriptor.GetConverter (type), new DefaultValidator(), flags, null)
61                 { }
62                 
63                 public ConfigurationProperty (
64                                         string name, Type type, object default_value,
65                                         TypeConverter converter,
66                                         ConfigurationValidatorBase validation,
67                                         ConfigurationPropertyOptions flags)
68                         : this (name, type, default_value, converter, validation, flags, null)
69                 { }
70
71                 public ConfigurationProperty (
72                                         string name, Type type, object default_value,
73                                         TypeConverter converter,
74                                         ConfigurationValidatorBase validation,
75                                         ConfigurationPropertyOptions flags,
76                                         string description)
77                 {
78                         this.name = name;
79                         this.converter = converter != null ? converter : TypeDescriptor.GetConverter (type);
80                         if (default_value != null) {
81                                 if (default_value == NoDefaultValue) {
82                                         switch (Type.GetTypeCode (type)) {
83                                         case TypeCode.Object:
84                                                 default_value = null;
85                                                 break;
86                                         case TypeCode.String:
87                                                 default_value = String.Empty;
88                                                 break;
89                                         default:
90                                                 default_value = Activator.CreateInstance (type);
91                                                 break;
92                                         }
93                                 }
94                                 else
95                                         if (!type.IsAssignableFrom (default_value.GetType ())) {
96                                                 if (!this.converter.CanConvertFrom (default_value.GetType ()))
97                                                         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}",
98                                                                                                            name, type, default_value.GetType ()));
99
100                                                 default_value = this.converter.ConvertFrom (default_value);
101                                         }
102                         }
103                         this.default_value = default_value;
104                         this.flags = flags;
105                         this.type = type;
106                         this.validation = validation != null ? validation : new DefaultValidator ();
107                         this.description = description;
108                 }
109
110                 public TypeConverter Converter {
111                         get { return converter; }
112                 }
113
114                 public object DefaultValue {                        
115                         get { return default_value; }
116                 }
117
118                 public bool IsKey {                        
119                         get { return (flags & ConfigurationPropertyOptions.IsKey) != 0; }
120                 }
121
122                 public bool IsRequired {
123                         get { return (flags & ConfigurationPropertyOptions.IsRequired) != 0; }
124                 }
125
126                 public bool IsDefaultCollection {
127                         get { return (flags & ConfigurationPropertyOptions.IsDefaultCollection) != 0; }
128                 }
129
130                 public string Name {
131                         get { return name; }
132                 }
133
134                 public string Description {
135                         get { return description; }
136                 }
137
138                 public Type Type {
139                         get { return type; }
140                 }
141
142                 public ConfigurationValidatorBase Validator {
143                         get { return validation; }
144                 }
145
146                 internal object ConvertFromString (string value)
147                 {
148                         if (converter != null)
149                                 return converter.ConvertFromInvariantString (value);
150                         else
151                                 throw new NotImplementedException ();
152                 }
153
154                 internal string ConvertToString (object value)
155                 {
156                         if (converter != null)
157                                 return converter.ConvertToInvariantString (value);
158                         else
159                                 throw new NotImplementedException ();
160                 }
161                 
162                 internal bool IsElement {
163                         get {
164                                 return (typeof(ConfigurationElement).IsAssignableFrom (type));
165                         }
166                 }
167                 
168                 internal ConfigurationCollectionAttribute CollectionAttribute {
169                         get { return collectionAttribute; }
170                         set { collectionAttribute = value; }
171                 }
172
173                 internal void Validate (object value)
174                 {
175                         if (validation != null)
176                                 validation.Validate (value);
177                 }
178         }
179 }
180 #endif