Do not skip comments, just pluck expressions/tags from within them
[mono.git] / mcs / class / System.Web / System.Web.Compilation / AppSettingsExpressionBuilder.cs
index 23e30849c01a59caafb17235ee6537cf88194109..ccfc79571c552a9a72bd787f696d19ce14053b1e 100644 (file)
@@ -36,6 +36,7 @@ using System.ComponentModel;
 using System.Configuration;
 using System.Web.Configuration;
 using System.Web.UI;
+using System.Reflection;
 
 namespace System.Web.Compilation {
 
@@ -53,18 +54,29 @@ namespace System.Web.Compilation {
                        string value = WebConfigurationManager.AppSettings [key];
 
                        if (value == null)
-                               throw new InvalidOperationException (String.Format ("App setting {0} not found", key));
+                               throw new InvalidOperationException (String.Format ("The application setting '{0}' was not found.", key));
                        return value;
                }
 
                public static object GetAppSetting (string key, Type targetType, string propertyName)
                {
+                       object value = GetAppSetting (key);
+
+                       if (targetType == null)
+                               return value.ToString ();
+
+                       PropertyInfo pi = targetType.GetProperty(propertyName);
+                       if (pi == null)
+                               return value.ToString ();
+
                        try {
-                               TypeConverter converter = TypeDescriptor.GetConverter (targetType);
-                               return converter.ConvertFrom (GetAppSetting (key));
-                       }
-                       catch (NotSupportedException e) {
-                               throw new InvalidOperationException (String.Format ("Could not convert app setting {0} to type {1}", key, targetType));
+                               TypeConverter converter = TypeDescriptor.GetConverter (pi.PropertyType);
+                               return converter.ConvertFrom (value);
+                       } catch (NotSupportedException) {
+                               throw new InvalidOperationException (String.Format (
+                                       "Could not convert application setting '{0}' " +
+                                       " to type '{1}' for property '{2}'.", value,
+                                       pi.PropertyType.Name, pi.Name));
                        }
                }
 
@@ -75,7 +87,7 @@ namespace System.Web.Compilation {
                        PropertyDescriptor descriptor = TypeDescriptor.GetProperties(type)[entry.PropertyInfo.Name];
                        CodeExpression[] expressionArray = new CodeExpression[3];
                        expressionArray[0] = new CodePrimitiveExpression(entry.Expression.Trim());
-                       expressionArray[1] = new CodeTypeOfExpression(type);
+                       expressionArray[1] = new CodeTypeOfExpression(entry.Type);
                        expressionArray[2] = new CodePrimitiveExpression(entry.Name);
                        return new CodeCastExpression(descriptor.PropertyType, new CodeMethodInvokeExpression(new 
                                                                       CodeTypeReferenceExpression(base.GetType()), "GetAppSetting", expressionArray));
@@ -89,5 +101,3 @@ namespace System.Web.Compilation {
 }
 
 #endif
-
-