2 // System.Configuration.ConfigurationProperty.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Lluis Sanchez Gual (lluis@novell.com)
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:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
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.
27 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
31 using System.ComponentModel;
33 namespace System.Configuration
35 public sealed class ConfigurationProperty
37 internal static readonly object NoDefaultValue = new object ();
42 TypeConverter converter;
43 ConfigurationValidatorBase validation;
44 ConfigurationPropertyOptions flags;
46 ConfigurationCollectionAttribute collectionAttribute;
48 public ConfigurationProperty (string name, Type type)
49 : this (name, type, NoDefaultValue, TypeDescriptor.GetConverter (type), new DefaultValidator(), ConfigurationPropertyOptions.None, null)
52 public ConfigurationProperty (string name, Type type, object default_value)
53 : this (name, type, default_value, TypeDescriptor.GetConverter (type), new DefaultValidator(), ConfigurationPropertyOptions.None, null)
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)
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)
70 public ConfigurationProperty (
71 string name, Type type, object default_value,
72 TypeConverter converter,
73 ConfigurationValidatorBase validation,
74 ConfigurationPropertyOptions flags,
78 this.converter = converter != null ? converter : TypeDescriptor.GetConverter (type);
79 if (default_value != null) {
80 if (default_value == NoDefaultValue) {
81 switch (Type.GetTypeCode (type)) {
86 default_value = String.Empty;
89 default_value = Activator.CreateInstance (type);
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 ()));
99 default_value = this.converter.ConvertFrom (default_value);
102 this.default_value = default_value;
105 this.validation = validation != null ? validation : new DefaultValidator ();
106 this.description = description;
109 public TypeConverter Converter {
110 get { return converter; }
113 public object DefaultValue {
114 get { return default_value; }
118 get { return (flags & ConfigurationPropertyOptions.IsKey) != 0; }
121 public bool IsRequired {
122 get { return (flags & ConfigurationPropertyOptions.IsRequired) != 0; }
125 public bool IsDefaultCollection {
126 get { return (flags & ConfigurationPropertyOptions.IsDefaultCollection) != 0; }
133 public string Description {
134 get { return description; }
141 public ConfigurationValidatorBase Validator {
142 get { return validation; }
145 internal object ConvertFromString (string value)
147 if (converter != null)
148 return converter.ConvertFromInvariantString (value);
150 throw new NotImplementedException ();
153 internal string ConvertToString (object value)
155 if (converter != null)
156 return converter.ConvertToInvariantString (value);
158 throw new NotImplementedException ();
161 internal bool IsElement {
163 return (typeof(ConfigurationElement).IsAssignableFrom (type));
167 internal ConfigurationCollectionAttribute CollectionAttribute {
168 get { return collectionAttribute; }
169 set { collectionAttribute = value; }
172 internal void Validate (object value)
174 if (validation != null)
175 validation.Validate (value);