Implementation of the 2.0 session state model
[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                                         throw new ConfigurationErrorsException (String.Format ("The default value for property '{0}' has a different type than the one of the property itself",
97                                                                                                    name));
98                         }
99                         this.default_value = default_value;
100                         this.flags = flags;
101                         this.type = type;
102                         this.validation = validation != null ? validation : new DefaultValidator ();
103                         this.description = description;
104                 }
105
106                 public TypeConverter Converter {
107                         get { return converter; }
108                 }
109
110                 public object DefaultValue {                        
111                         get { return default_value; }
112                 }
113
114                 public bool IsKey {                        
115                         get { return (flags & ConfigurationPropertyOptions.IsKey) != 0; }
116                 }
117
118                 public bool IsRequired {
119                         get { return (flags & ConfigurationPropertyOptions.IsRequired) != 0; }
120                 }
121
122                 public bool IsDefaultCollection {
123                         get { return (flags & ConfigurationPropertyOptions.IsDefaultCollection) != 0; }
124                 }
125
126                 public string Name {
127                         get { return name; }
128                 }
129
130                 public string Description {
131                         get { return description; }
132                 }
133
134                 public Type Type {
135                         get { return type; }
136                 }
137
138                 public ConfigurationValidatorBase Validator {
139                         get { return validation; }
140                 }
141
142                 internal object ConvertFromString (string value)
143                 {
144                         if (converter != null)
145                                 return converter.ConvertFromInvariantString (value);
146                         else
147                                 throw new NotImplementedException ();
148                 }
149
150                 internal string ConvertToString (object value)
151                 {
152                         if (converter != null)
153                                 return converter.ConvertToInvariantString (value);
154                         else
155                                 throw new NotImplementedException ();
156                 }
157                 
158                 internal bool IsElement {
159                         get {
160                                 return (typeof(ConfigurationElement).IsAssignableFrom (type));
161                         }
162                 }
163                 
164                 internal ConfigurationCollectionAttribute CollectionAttribute {
165                         get { return collectionAttribute; }
166                         set { collectionAttribute = value; }
167                 }
168
169                 internal void Validate (object value)
170                 {
171                         if (validation != null)
172                                 validation.Validate (value);
173                 }
174         }
175 }
176 #endif