2007-04-17 Atsushi Enomoto <atsushi@ximian.com>
[mono.git] / mcs / class / System.Configuration / System.Configuration / ConfigurationProperty.cs
index 42d3d2385d2c6f2499c24c3d375317f9aefc82d0..93a2c5d1123ae447717e6a84569ddaf97a667112 100644 (file)
@@ -35,11 +35,11 @@ namespace System.Configuration
 {
        public sealed class ConfigurationProperty
        {
-               internal static object NoDefaultValue = new object ();
+               internal static readonly object NoDefaultValue = new object ();
                
                string name;
                Type type;
-               object default_value = NoDefaultValue;
+               object default_value;
                TypeConverter converter;
                ConfigurationValidatorBase validation;
                ConfigurationPropertyOptions flags;
@@ -76,11 +76,34 @@ namespace System.Configuration
                                        string description)
                {
                        this.name = name;
-                       this.converter = converter;
+                       this.converter = converter != null ? converter : TypeDescriptor.GetConverter (type);
+                       if (default_value != null) {
+                               if (default_value == NoDefaultValue) {
+                                       switch (Type.GetTypeCode (type)) {
+                                       case TypeCode.Object:
+                                               default_value = null;
+                                               break;
+                                       case TypeCode.String:
+                                               default_value = String.Empty;
+                                               break;
+                                       default:
+                                               default_value = Activator.CreateInstance (type);
+                                               break;
+                                       }
+                               }
+                               else
+                                       if (!type.IsAssignableFrom (default_value.GetType ())) {
+                                               if (!this.converter.CanConvertFrom (default_value.GetType ()))
+                                                       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}",
+                                                                                                          name, type, default_value.GetType ()));
+
+                                               default_value = this.converter.ConvertFrom (default_value);
+                                       }
+                       }
                        this.default_value = default_value;
                        this.flags = flags;
                        this.type = type;
-                       this.validation = validation;
+                       this.validation = validation != null ? validation : new DefaultValidator ();
                        this.description = description;
                }