--- /dev/null
+/**\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
+ private 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(Exception e)\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(enumType.IsEnum)\r
+ {\r
+ return EnumFromString(objType, objValue);\r
+ }\r
+ if(enumType.Equals(typeof(string)))\r
+ {\r
+ return objValue;\r
+ }\r
+ PropertyDescriptor pc = null;\r
+ if(propertyInfo != null)\r
+ {\r
+ pc = (TypeDesctiptor.GetProperties(propertyInfo.ReflectedType))[propertyInfo.Name];\r
+ }\r
+ if(pc != null)\r
+ {\r
+ TypeConverter converter = pc.Converter;\r
+ if(converter!=null && conveter.CanConverFrom(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 = Util.InvokeMethod(propertyInfo, null, parameters);\r
+ } catch(Exception e)\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 = Util.InvokeMethod(propertyInfo, null, parameters);\r
+ } catch(Exception e)\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
--- /dev/null
+/**\r
+ * Namespace: System.Web.UI\r
+ * Class: Utils\r
+ * \r
+ * Author: Gaurav Vaish\r
+ * Maintainer: gvaish@iitk.ac.in\r
+ * Implementation: yes\r
+ * Contact: <gvaish@iitk.ac.in>\r
+ * Status: ?%\r
+ * \r
+ * (C) Gaurav Vaish (2001)\r
+ */\r
+\r
+using System;\r
+using System.Reflection;\r
+\r
+namespace System.Web.UI\r
+{\r
+ private class Utils\r
+ {\r
+ internal static object InvokeMethod(MethodInfo info, object obj, object[] parameters)\r
+ {\r
+ object retVal = null;\r
+ try\r
+ {\r
+ retVal = info.Invoke(obj, parameters);\r
+ } catch(TargetInvocationException tie)\r
+ {\r
+ throw tie.InnerException;\r
+ }\r
+ return retVal;\r
+ }\r
+ }\r
+}\r