3 // Permission is hereby granted, free of charge, to any person obtaining
4 // a copy of this software and associated documentation files (the
5 // "Software"), to deal in the Software without restriction, including
6 // without limitation the rights to use, copy, modify, merge, publish,
7 // distribute, sublicense, and/or sell copies of the Software, and to
8 // permit persons to whom the Software is furnished to do so, subject to
9 // the following conditions:
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 * Namespace: System.Web.UI
\r
24 * Class: PropertyConverter
\r
26 * Author: Gaurav Vaish
\r
27 * Maintainer: gvaish@iitk.ac.in
\r
28 * Implementation: yes
\r
29 * Contact: <gvaish@iitk.ac.in>
\r
32 * (C) Gaurav Vaish (2001)
\r
36 using System.ComponentModel;
\r
37 using System.Globalization;
\r
38 using System.Reflection;
\r
40 namespace System.Web.UI
\r
42 public sealed class PropertyConverter
\r
44 private static Type[] parseMethodTypes;
\r
45 private static Type[] parseMethodTypesWithSOP;
\r
47 static PropertyConverter()
\r
49 parseMethodTypes = new Type[1];
\r
50 parseMethodTypes[0] = typeof(string);
\r
51 parseMethodTypesWithSOP = new Type[2];
\r
52 parseMethodTypesWithSOP[0] = typeof(string);
\r
53 parseMethodTypesWithSOP[1] = typeof(IServiceProvider);
\r
56 private PropertyConverter()
\r
58 // Prevent any instance
\r
61 public static object EnumFromString(Type enumType, string enumValue)
\r
63 object retVal = null;
\r
66 retVal = Enum.Parse(enumType, enumValue, true);
\r
74 public static string EnumToString(Type enumType, object enumValue)
\r
76 string retVal = Enum.Format(enumType, enumValue, "G");
\r
77 return retVal.Replace('_','-');
\r
80 public static object ObjectFromString(Type objType, MemberInfo propertyInfo, string objValue)
\r
82 if(objValue == null)
\r
84 if(! (!objType.Equals(typeof(Boolean)) || objValue.Length > 0) )
\r
90 return EnumFromString(objType, objValue);
\r
92 if(objType.Equals(typeof(string)))
\r
96 PropertyDescriptor pc = null;
\r
97 if(propertyInfo != null)
\r
99 pc = (TypeDescriptor.GetProperties(propertyInfo.ReflectedType))[propertyInfo.Name];
\r
103 TypeConverter converter = pc.Converter;
\r
104 if(converter!=null && converter.CanConvertFrom(typeof(string)))
\r
106 return converter.ConvertFromInvariantString(objValue);
\r
109 MethodInfo mi = objType.GetMethod("Parse", parseMethodTypesWithSOP);
\r
113 object[] parameters = new object[2];
\r
114 parameters[0] = objValue;
\r
115 parameters[1] = CultureInfo.InvariantCulture;
\r
118 o = Utils.InvokeMethod(mi, null, parameters);
\r
125 mi = objType.GetMethod("Parse", parseMethodTypes);
\r
128 object[] parameters = new object[1];
\r
129 parameters[0] = objValue;
\r
132 o = Utils.InvokeMethod(mi, null, parameters);
\r
140 throw new HttpException(/*HttpRuntime.FormatResourceString(*/"Type_not_creatable_from_string"/*, objType.FullName, objValue, propertyInfo.Name)*/);
\r