diff options
author | Gaurav Vaish <gvaish@mono-cvs.ximian.com> | 2001-12-16 21:17:35 +0300 |
---|---|---|
committer | Gaurav Vaish <gvaish@mono-cvs.ximian.com> | 2001-12-16 21:17:35 +0300 |
commit | ef8bc3930f89f03cb77217d89056d7f5871c9398 (patch) | |
tree | d4b8f9cc4b5fcc0445870c3dfb9f88f8c7aac99b /mcs/class/System.Web/System.Web.UI | |
parent | 15b98547ac443bc45affcda54bbd123a40148565 (diff) |
2001-12-17 Gaurav Vaish <Gvaish@iitk.ac.in>
* PropertyConverter -- I needed it. Completed
* Utils -- Also needed this. Initial implementation
svn path=/trunk/mcs/; revision=1593
Diffstat (limited to 'mcs/class/System.Web/System.Web.UI')
-rw-r--r-- | mcs/class/System.Web/System.Web.UI/PropertyConverter.cs | 124 | ||||
-rw-r--r-- | mcs/class/System.Web/System.Web.UI/Utils.cs | 34 |
2 files changed, 158 insertions, 0 deletions
diff --git a/mcs/class/System.Web/System.Web.UI/PropertyConverter.cs b/mcs/class/System.Web/System.Web.UI/PropertyConverter.cs new file mode 100644 index 00000000000..84d62f08701 --- /dev/null +++ b/mcs/class/System.Web/System.Web.UI/PropertyConverter.cs @@ -0,0 +1,124 @@ +/**
+ * Namespace: System.Web.UI
+ * Class: PropertyConverter
+ *
+ * Author: Gaurav Vaish
+ * Maintainer: gvaish@iitk.ac.in
+ * Implementation: yes
+ * Contact: <gvaish@iitk.ac.in>
+ * Status: 100%
+ *
+ * (C) Gaurav Vaish (2001)
+ */
+
+using System;
+using System.ComponentModel;
+using System.Globalization;
+using System.Reflection;
+
+namespace System.Web.UI
+{
+ public sealed class PropertyConverter
+ {
+ private static Type[] parseMethodTypes;
+ private static Type[] parseMethodTypesWithSOP;
+
+ private static PropertyConverter()
+ {
+ parseMethodTypes = new Type[1];
+ parseMethodTypes[0] = typeof(string);
+ parseMethodTypesWithSOP = new Type[2];
+ parseMethodTypesWithSOP[0] = typeof(string);
+ parseMethodTypesWithSOP[1] = typeof(IServiceProvider);
+ }
+
+ private PropertyConverter()
+ {
+ // Prevent any instance
+ }
+
+ public static object EnumFromString(Type enumType, string enumValue)
+ {
+ object retVal = null;
+ try
+ {
+ retVal = Enum.Parse(enumType, enumValue, true);
+ } catch(Exception e)
+ {
+ retVal = null;
+ }
+ return retVal;
+ }
+
+ public static string EnumToString(Type enumType, object enumValue)
+ {
+ string retVal = Enum.Format(enumType, enumValue, "G");
+ return retVal.Replace('_','-');
+ }
+
+ public static object ObjectFromString(Type objType, MemberInfo propertyInfo, string objValue)
+ {
+ if(objValue == null)
+ return null;
+ if(! (!objType.Equals(typeof(Boolean)) || objValue.Length > 0) )
+ {
+ return null;
+ }
+ if(enumType.IsEnum)
+ {
+ return EnumFromString(objType, objValue);
+ }
+ if(enumType.Equals(typeof(string)))
+ {
+ return objValue;
+ }
+ PropertyDescriptor pc = null;
+ if(propertyInfo != null)
+ {
+ pc = (TypeDesctiptor.GetProperties(propertyInfo.ReflectedType))[propertyInfo.Name];
+ }
+ if(pc != null)
+ {
+ TypeConverter converter = pc.Converter;
+ if(converter!=null && conveter.CanConverFrom(typeof(string)))
+ {
+ return converter.ConvertFromInvariantString(objValue);
+ }
+ }
+ MethodInfo mi = objType.GetMethod("Parse", parseMethodTypesWithSOP);
+ object o = null;
+ if(mi != null)
+ {
+ object[] parameters = new object[2];
+ parameters[0] = objValue;
+ parameters[1] = CultureInfo.InvariantCulture;
+ try
+ {
+ o = Util.InvokeMethod(propertyInfo, null, parameters);
+ } catch(Exception e)
+ {
+ }
+ }
+ if(o == null)
+ {
+ mi = objType.GetMethod("Parse", parseMethodTypes);
+ if(mi!=null)
+ {
+ object[] parameters = new object[1];
+ parameters[0] = objValue;
+ try
+ {
+ o = Util.InvokeMethod(propertyInfo, null, parameters);
+ } catch(Exception e)
+ {
+ }
+ }
+ }
+ if(o == null)
+ {
+ throw new HttpException(HttpRuntime.FormatResourceString("Type_not_creatable_from_string", objType.FullName, objValue, propertyInfo.Name);
+ }
+ return o;
+ }
+ }
+}
diff --git a/mcs/class/System.Web/System.Web.UI/Utils.cs b/mcs/class/System.Web/System.Web.UI/Utils.cs new file mode 100644 index 00000000000..e696e75e44d --- /dev/null +++ b/mcs/class/System.Web/System.Web.UI/Utils.cs @@ -0,0 +1,34 @@ +/**
+ * Namespace: System.Web.UI
+ * Class: Utils
+ *
+ * Author: Gaurav Vaish
+ * Maintainer: gvaish@iitk.ac.in
+ * Implementation: yes
+ * Contact: <gvaish@iitk.ac.in>
+ * Status: ?%
+ *
+ * (C) Gaurav Vaish (2001)
+ */
+
+using System;
+using System.Reflection;
+
+namespace System.Web.UI
+{
+ private class Utils
+ {
+ internal static object InvokeMethod(MethodInfo info, object obj, object[] parameters)
+ {
+ object retVal = null;
+ try
+ {
+ retVal = info.Invoke(obj, parameters);
+ } catch(TargetInvocationException tie)
+ {
+ throw tie.InnerException;
+ }
+ return retVal;
+ }
+ }
+}
|