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)
32 using System.ComponentModel;
34 namespace System.Configuration
36 public sealed class ConfigurationProperty
38 internal static readonly object NoDefaultValue = new object ();
43 TypeConverter converter;
44 ConfigurationValidatorBase validation;
45 ConfigurationPropertyOptions flags;
47 ConfigurationCollectionAttribute collectionAttribute;
49 public ConfigurationProperty (string name, Type type)
50 : this (name, type, NoDefaultValue, TypeDescriptor.GetConverter (type), new DefaultValidator(), ConfigurationPropertyOptions.None, null)
53 public ConfigurationProperty (string name, Type type, object default_value)
54 : this (name, type, default_value, TypeDescriptor.GetConverter (type), new DefaultValidator(), ConfigurationPropertyOptions.None, null)
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)
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)
71 public ConfigurationProperty (
72 string name, Type type, object default_value,
73 TypeConverter converter,
74 ConfigurationValidatorBase validation,
75 ConfigurationPropertyOptions flags,
79 this.converter = converter != null ? converter : TypeDescriptor.GetConverter (type);
80 if (default_value != null) {
81 if (default_value == NoDefaultValue) {
82 switch (Type.GetTypeCode (type)) {
87 default_value = String.Empty;
90 default_value = Activator.CreateInstance (type);
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",
99 this.default_value = default_value;
102 this.validation = validation != null ? validation : new DefaultValidator ();
103 this.description = description;
106 public TypeConverter Converter {
107 get { return converter; }
110 public object DefaultValue {
111 get { return default_value; }
115 get { return (flags & ConfigurationPropertyOptions.IsKey) != 0; }
118 public bool IsRequired {
119 get { return (flags & ConfigurationPropertyOptions.IsRequired) != 0; }
122 public bool IsDefaultCollection {
123 get { return (flags & ConfigurationPropertyOptions.IsDefaultCollection) != 0; }
130 public string Description {
131 get { return description; }
138 public ConfigurationValidatorBase Validator {
139 get { return validation; }
142 internal object ConvertFromString (string value)
144 if (converter != null)
145 return converter.ConvertFromInvariantString (value);
147 throw new NotImplementedException ();
150 internal string ConvertToString (object value)
152 if (converter != null)
153 return converter.ConvertToInvariantString (value);
155 throw new NotImplementedException ();
158 internal bool IsElement {
160 return (typeof(ConfigurationElement).IsAssignableFrom (type));
164 internal ConfigurationCollectionAttribute CollectionAttribute {
165 get { return collectionAttribute; }
166 set { collectionAttribute = value; }
169 internal void Validate (object value)
171 if (validation != null)
172 validation.Validate (value);