using System.Configuration;
using System.Web.Configuration;
using System.Web.UI;
+using System.Reflection;
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));
}
}
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));
}
#endif
-
-