(DISTFILES): Comment out a few missing files.
[mono-project.git] / mcs / class / System.Web / System.Web.UI / PropertyConverter.cs
blobe9ba5cde1f87f8ec5821931bfd9d1004f5a47488
2 //
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:
10 //
11 // The above copyright notice and this permission notice shall be
12 // included in all copies or substantial portions of the Software.
13 //
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.
22 /**
23 * Namespace: System.Web.UI
24 * Class: PropertyConverter
26 * Author: Gaurav Vaish
27 * Maintainer: gvaish@iitk.ac.in
28 * Implementation: yes
29 * Contact: <gvaish@iitk.ac.in>
30 * Status: 100%
32 * (C) Gaurav Vaish (2001)
35 using System;
36 using System.ComponentModel;
37 using System.Globalization;
38 using System.Reflection;
40 namespace System.Web.UI
42 public sealed class PropertyConverter
44 private static Type[] parseMethodTypes;
45 private static Type[] parseMethodTypesWithSOP;
47 static PropertyConverter()
49 parseMethodTypes = new Type[1];
50 parseMethodTypes[0] = typeof(string);
51 parseMethodTypesWithSOP = new Type[2];
52 parseMethodTypesWithSOP[0] = typeof(string);
53 parseMethodTypesWithSOP[1] = typeof(IServiceProvider);
56 private PropertyConverter()
58 // Prevent any instance
61 public static object EnumFromString(Type enumType, string enumValue)
63 object retVal = null;
64 try
66 retVal = Enum.Parse(enumType, enumValue, true);
67 } catch
69 retVal = null;
71 return retVal;
74 public static string EnumToString(Type enumType, object enumValue)
76 string retVal = Enum.Format(enumType, enumValue, "G");
77 return retVal.Replace('_','-');
80 public static object ObjectFromString(Type objType, MemberInfo propertyInfo, string objValue)
82 if(objValue == null)
83 return null;
84 if(! (!objType.Equals(typeof(Boolean)) || objValue.Length > 0) )
86 return null;
88 if(objType.IsEnum)
90 return EnumFromString(objType, objValue);
92 if(objType.Equals(typeof(string)))
94 return objValue;
96 PropertyDescriptor pc = null;
97 if(propertyInfo != null)
99 pc = (TypeDescriptor.GetProperties(propertyInfo.ReflectedType))[propertyInfo.Name];
101 if(pc != null)
103 TypeConverter converter = pc.Converter;
104 if(converter!=null && converter.CanConvertFrom(typeof(string)))
106 return converter.ConvertFromInvariantString(objValue);
109 MethodInfo mi = objType.GetMethod("Parse", parseMethodTypesWithSOP);
110 object o = null;
111 if(mi != null)
113 object[] parameters = new object[2];
114 parameters[0] = objValue;
115 parameters[1] = CultureInfo.InvariantCulture;
118 o = Utils.InvokeMethod(mi, null, parameters);
119 } catch
123 if(o == null)
125 mi = objType.GetMethod("Parse", parseMethodTypes);
126 if(mi!=null)
128 object[] parameters = new object[1];
129 parameters[0] = objValue;
132 o = Utils.InvokeMethod(mi, null, parameters);
133 } catch
138 if(o == null)
140 throw new HttpException(/*HttpRuntime.FormatResourceString(*/"Type_not_creatable_from_string"/*, objType.FullName, objValue, propertyInfo.Name)*/);
142 return o;