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
24 * Class: PropertyConverter
26 * Author: Gaurav Vaish
27 * Maintainer: gvaish@iitk.ac.in
29 * Contact: <gvaish@iitk.ac.in>
32 * (C) Gaurav Vaish (2001)
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
)
66 retVal
= Enum
.Parse(enumType
, enumValue
, true);
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
)
84 if(! (!objType
.Equals(typeof(Boolean
)) || objValue
.Length
> 0) )
90 return EnumFromString(objType
, objValue
);
92 if(objType
.Equals(typeof(string)))
96 PropertyDescriptor pc
= null;
97 if(propertyInfo
!= null)
99 pc
= (TypeDescriptor
.GetProperties(propertyInfo
.ReflectedType
))[propertyInfo
.Name
];
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
);
113 object[] parameters
= new object[2];
114 parameters
[0] = objValue
;
115 parameters
[1] = CultureInfo
.InvariantCulture
;
118 o
= Utils
.InvokeMethod(mi
, null, parameters
);
125 mi
= objType
.GetMethod("Parse", parseMethodTypes
);
128 object[] parameters
= new object[1];
129 parameters
[0] = objValue
;
132 o
= Utils
.InvokeMethod(mi
, null, parameters
);
140 throw new HttpException(/*HttpRuntime.FormatResourceString(*/"Type_not_creatable_from_string"/*, objType.FullName, objValue, propertyInfo.Name)*/);