2002-10-25 Gonzalo Paniagua Javier <gonzalo@ximian.com>
[mono.git] / mcs / class / System.Web / System.Web.UI / PropertyConverter.cs
1 /**\r
2  * Namespace: System.Web.UI\r
3  * Class:     PropertyConverter\r
4  *\r
5  * Author:  Gaurav Vaish\r
6  * Maintainer: gvaish@iitk.ac.in\r
7  * Implementation: yes\r
8  * Contact: <gvaish@iitk.ac.in>\r
9  * Status:  100%\r
10  *\r
11  * (C) Gaurav Vaish (2001)\r
12  */\r
13 \r
14 using System;\r
15 using System.ComponentModel;\r
16 using System.Globalization;\r
17 using System.Reflection;\r
18 \r
19 namespace System.Web.UI\r
20 {\r
21         public sealed class PropertyConverter\r
22         {\r
23                 private static Type[] parseMethodTypes;\r
24                 private static Type[] parseMethodTypesWithSOP;\r
25 \r
26                 static PropertyConverter()\r
27                 {\r
28                         parseMethodTypes = new Type[1];\r
29                         parseMethodTypes[0] = typeof(string);\r
30                         parseMethodTypesWithSOP = new Type[2];\r
31                         parseMethodTypesWithSOP[0] = typeof(string);\r
32                         parseMethodTypesWithSOP[1] = typeof(IServiceProvider);\r
33                 }\r
34 \r
35                 private PropertyConverter()\r
36                 {\r
37                         // Prevent any instance\r
38                 }\r
39 \r
40                 public static object EnumFromString(Type enumType, string enumValue)\r
41                 {\r
42                         object retVal = null;\r
43                         try\r
44                         {\r
45                                 retVal = Enum.Parse(enumType, enumValue, true);\r
46                         } catch\r
47                         {\r
48                                 retVal = null;\r
49                         }\r
50                         return retVal;\r
51                 }\r
52 \r
53                 public static string EnumToString(Type enumType, object enumValue)\r
54                 {\r
55                         string retVal = Enum.Format(enumType, enumValue, "G");\r
56                         return retVal.Replace('_','-');\r
57                 }\r
58 \r
59                 public static object ObjectFromString(Type objType, MemberInfo propertyInfo, string objValue)\r
60                 {\r
61                         if(objValue == null)\r
62                                 return null;\r
63                         if(! (!objType.Equals(typeof(Boolean)) || objValue.Length > 0) )\r
64                         {\r
65                                 return null;\r
66                         }\r
67                         if(objType.IsEnum)\r
68                         {\r
69                                 return EnumFromString(objType, objValue);\r
70                         }\r
71                         if(objType.Equals(typeof(string)))\r
72                         {\r
73                                 return objValue;\r
74                         }\r
75                         PropertyDescriptor pc = null;\r
76                         if(propertyInfo != null)\r
77                         {\r
78                                 pc = (TypeDescriptor.GetProperties(propertyInfo.ReflectedType))[propertyInfo.Name];\r
79                         }\r
80                         if(pc != null)\r
81                         {\r
82                                 TypeConverter converter = pc.Converter;\r
83                                 if(converter!=null && converter.CanConvertFrom(typeof(string)))\r
84                                 {\r
85                                         return converter.ConvertFromInvariantString(objValue);\r
86                                 }\r
87                         }\r
88                         MethodInfo mi = objType.GetMethod("Parse", parseMethodTypesWithSOP);\r
89                         object o = null;\r
90                         if(mi != null)\r
91                         {\r
92                                 object[] parameters = new object[2];\r
93                                 parameters[0] = objValue;\r
94                                 parameters[1] = CultureInfo.InvariantCulture;\r
95                                 try\r
96                                 {\r
97                                         o = Utils.InvokeMethod(mi, null, parameters);\r
98                                 } catch\r
99                                 {\r
100                                 }\r
101                         }\r
102                         if(o == null)\r
103                         {\r
104                                 mi = objType.GetMethod("Parse", parseMethodTypes);\r
105                                 if(mi!=null)\r
106                                 {\r
107                                         object[] parameters = new object[1];\r
108                                         parameters[0] = objValue;\r
109                                         try\r
110                                         {\r
111                                                 o = Utils.InvokeMethod(mi, null, parameters);\r
112                                         } catch\r
113                                         {\r
114                                         }\r
115                                 }\r
116                         }\r
117                         if(o == null)\r
118                         {\r
119                                 throw new HttpException(/*HttpRuntime.FormatResourceString(*/"Type_not_creatable_from_string"/*, objType.FullName, objValue, propertyInfo.Name)*/);\r
120                         }\r
121                         return o;\r
122                 }\r
123         }\r
124 }\r