2007-05-15 Marek Habersack <mhabersack@novell.com>
authorMarek Habersack <grendel@twistedcode.net>
Tue, 15 May 2007 08:07:45 +0000 (08:07 -0000)
committerMarek Habersack <grendel@twistedcode.net>
Tue, 15 May 2007 08:07:45 +0000 (08:07 -0000)
* ConfigurationElement.cs: added value validation on
deserialization from the config file.

svn path=/trunk/mcs/; revision=77394

mcs/class/System.Configuration/System.Configuration/ChangeLog
mcs/class/System.Configuration/System.Configuration/ConfigurationElement.cs

index 2d59de6392adda00cf0c52fa1f19de24f0f4beaf..deb4492a1b5b14e1c0e389c69cc3666675983db8 100644 (file)
@@ -1,3 +1,8 @@
+2007-05-15  Marek Habersack  <mhabersack@novell.com>
+
+       * ConfigurationElement.cs: added value validation on
+       deserialization from the config file.
+
 2007-05-14 Igor Zelmanovich <igorz@mainsoft.com>
 
        * ConfigurationElement.cs: when attribute value cannot be parsed,
index 428d74d42113f50b2033720c16a47bac5296ef16..6cfd5570f110909c156ea6ab7b4d09f64c209f1e 100644 (file)
@@ -310,11 +310,17 @@ namespace System.Configuration
                                
                                if (readProps.ContainsKey (prop))
                                        throw new ConfigurationException ("The attribute '" + prop.Name + "' may only appear once in this element.");
-                               
+
+                               string value = null;
                                try {
-                                       prop.SetStringValue (reader.Value);
-                               }
-                               catch (Exception ex) {
+                                       value = reader.Value;
+                                       ValidateValue (prop.Property, value);
+                                       prop.SetStringValue (value);
+                               } catch (ConfigurationErrorsException) {
+                                       throw;
+                               } catch (ConfigurationException) {
+                                       throw;
+                               } catch (Exception ex) {
                                        string msg = String.Format ("The value of the property '{0}' cannot be parsed.", prop.Name);
                                        throw new ConfigurationErrorsException (msg, reader);
                                }
@@ -324,9 +330,7 @@ namespace System.Configuration
                        reader.MoveToElement ();
                        if (reader.IsEmptyElement) {
                                reader.Skip ();
-                       }
-                       else {
-
+                       } else {
                                int depth = reader.Depth;
 
                                reader.ReadStartElement ();
@@ -538,6 +542,18 @@ namespace System.Configuration
                        PropertyInformation info = ElementInformation.Properties [propName];
                        return info != null && info.ValueOrigin == PropertyValueOrigin.SetHere;
                }
+
+               void ValidateValue (ConfigurationProperty p, string value)
+               {
+                       ConfigurationValidatorBase validator;
+                       if (p == null || (validator = p.Validator) == null)
+                               return;
+                       
+                       if (!validator.CanValidate (p.Type))
+                               throw new ConfigurationException (
+                                       String.Format ("Validator does not support type {0}", p.Type));
+                       validator.Validate (p.ConvertFromString (value));
+               }
        }
        
        internal class ElementMap