-
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
-/**\r
- * Namespace: System.Web.UI\r
- * Class: PropertyConverter\r
- *\r
- * Author: Gaurav Vaish\r
- * Maintainer: gvaish@iitk.ac.in\r
- * Implementation: yes\r
- * Contact: <gvaish@iitk.ac.in>\r
- * Status: 100%\r
- *\r
- * (C) Gaurav Vaish (2001)\r
- */\r
-\r
-using System;\r
-using System.ComponentModel;\r
-using System.Globalization;\r
-using System.Reflection;\r
-\r
-namespace System.Web.UI\r
-{\r
- public sealed class PropertyConverter\r
- {\r
- private static Type[] parseMethodTypes;\r
- private static Type[] parseMethodTypesWithSOP;\r
-\r
- static PropertyConverter()\r
- {\r
- parseMethodTypes = new Type[1];\r
- parseMethodTypes[0] = typeof(string);\r
- parseMethodTypesWithSOP = new Type[2];\r
- parseMethodTypesWithSOP[0] = typeof(string);\r
- parseMethodTypesWithSOP[1] = typeof(IServiceProvider);\r
- }\r
-\r
- private PropertyConverter()\r
- {\r
- // Prevent any instance\r
- }\r
-\r
- public static object EnumFromString(Type enumType, string enumValue)\r
- {\r
- object retVal = null;\r
- try\r
- {\r
- retVal = Enum.Parse(enumType, enumValue, true);\r
- } catch\r
- {\r
- retVal = null;\r
- }\r
- return retVal;\r
- }\r
-\r
- public static string EnumToString(Type enumType, object enumValue)\r
- {\r
- string retVal = Enum.Format(enumType, enumValue, "G");\r
- return retVal.Replace('_','-');\r
- }\r
-\r
- public static object ObjectFromString(Type objType, MemberInfo propertyInfo, string objValue)\r
- {\r
- if(objValue == null)\r
- return null;\r
- if(! (!objType.Equals(typeof(Boolean)) || objValue.Length > 0) )\r
- {\r
- return null;\r
- }\r
- if(objType.IsEnum)\r
- {\r
- return EnumFromString(objType, objValue);\r
- }\r
- if(objType.Equals(typeof(string)))\r
- {\r
- return objValue;\r
- }\r
- PropertyDescriptor pc = null;\r
- if(propertyInfo != null)\r
- {\r
- pc = (TypeDescriptor.GetProperties(propertyInfo.ReflectedType))[propertyInfo.Name];\r
- }\r
- if(pc != null)\r
- {\r
- TypeConverter converter = pc.Converter;\r
- if(converter!=null && converter.CanConvertFrom(typeof(string)))\r
- {\r
- return converter.ConvertFromInvariantString(objValue);\r
- }\r
- }\r
- MethodInfo mi = objType.GetMethod("Parse", parseMethodTypesWithSOP);\r
- object o = null;\r
- if(mi != null)\r
- {\r
- object[] parameters = new object[2];\r
- parameters[0] = objValue;\r
- parameters[1] = CultureInfo.InvariantCulture;\r
- try\r
- {\r
- o = Utils.InvokeMethod(mi, null, parameters);\r
- } catch\r
- {\r
- }\r
- }\r
- if(o == null)\r
- {\r
- mi = objType.GetMethod("Parse", parseMethodTypes);\r
- if(mi!=null)\r
- {\r
- object[] parameters = new object[1];\r
- parameters[0] = objValue;\r
- try\r
- {\r
- o = Utils.InvokeMethod(mi, null, parameters);\r
- } catch\r
- {\r
- }\r
- }\r
- }\r
- if(o == null)\r
- {\r
- throw new HttpException(/*HttpRuntime.FormatResourceString(*/"Type_not_creatable_from_string"/*, objType.FullName, objValue, propertyInfo.Name)*/);\r
- }\r
- return o;\r
- }\r
- }\r
-}\r
+//
+// System.Web.UI.PropertyConverter.cs
+//
+// Authors:
+// Jackson Harper (jackson@ximian.com)
+//
+// Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
+
+using System.Reflection;
+using System.ComponentModel;
+using System.Security.Permissions;
+
+namespace System.Web.UI {
+
+ // CAS - no InheritanceDemand here as the class is sealed
+ [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
+ public static class PropertyConverter
+ {
+ public static object EnumFromString (Type enumType, string value)
+ {
+ object res = null;
+
+ try {
+ res = Enum.Parse (enumType, value, true);
+ } catch {
+ res = null;
+ }
+ return res;
+ }
+
+ public static string EnumToString (Type enumType, object enumValue)
+ {
+ return Enum.Format (enumType, enumValue, "G");
+ }
+
+ public static object ObjectFromString (Type objType,
+ MemberInfo propertyInfo, string value)
+ {
+ if (objType == typeof (string))
+ return value;
+
+ // Is there a less kludgy way to get the converter?
+ PropertyDescriptorCollection col = TypeDescriptor.GetProperties (
+ propertyInfo.ReflectedType);
+ PropertyDescriptor pd = col.Find (propertyInfo.Name, false);
+ if (pd.Converter == null || !pd.Converter.CanConvertFrom (typeof (string))) {
+ throw new HttpException (Locale.GetText ("Cannot create an object " +
+ "of type '{0}' from its string representation '{1}' for the " +
+ "'{2}' property", objType, value, propertyInfo.Name));
+ }
+ return pd.Converter.ConvertFromInvariantString (value);
+ }
+ }
+}
+