PropertyConverter.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //
  2. // Permission is hereby granted, free of charge, to any person obtaining
  3. // a copy of this software and associated documentation files (the
  4. // "Software"), to deal in the Software without restriction, including
  5. // without limitation the rights to use, copy, modify, merge, publish,
  6. // distribute, sublicense, and/or sell copies of the Software, and to
  7. // permit persons to whom the Software is furnished to do so, subject to
  8. // the following conditions:
  9. //
  10. // The above copyright notice and this permission notice shall be
  11. // included in all copies or substantial portions of the Software.
  12. //
  13. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  14. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  15. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  16. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  17. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  18. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  19. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  20. //
  21. /**
  22. * Namespace: System.Web.UI
  23. * Class: PropertyConverter
  24. *
  25. * Author: Gaurav Vaish
  26. * Maintainer: [email protected]
  27. * Implementation: yes
  28. * Contact: <[email protected]>
  29. * Status: 100%
  30. *
  31. * (C) Gaurav Vaish (2001)
  32. */
  33. using System;
  34. using System.ComponentModel;
  35. using System.Globalization;
  36. using System.Reflection;
  37. namespace System.Web.UI
  38. {
  39. public sealed class PropertyConverter
  40. {
  41. private static Type[] parseMethodTypes;
  42. private static Type[] parseMethodTypesWithSOP;
  43. static PropertyConverter()
  44. {
  45. parseMethodTypes = new Type[1];
  46. parseMethodTypes[0] = typeof(string);
  47. parseMethodTypesWithSOP = new Type[2];
  48. parseMethodTypesWithSOP[0] = typeof(string);
  49. parseMethodTypesWithSOP[1] = typeof(IServiceProvider);
  50. }
  51. private PropertyConverter()
  52. {
  53. // Prevent any instance
  54. }
  55. public static object EnumFromString(Type enumType, string enumValue)
  56. {
  57. object retVal = null;
  58. try
  59. {
  60. retVal = Enum.Parse(enumType, enumValue, true);
  61. } catch
  62. {
  63. retVal = null;
  64. }
  65. return retVal;
  66. }
  67. public static string EnumToString(Type enumType, object enumValue)
  68. {
  69. string retVal = Enum.Format(enumType, enumValue, "G");
  70. return retVal.Replace('_','-');
  71. }
  72. public static object ObjectFromString(Type objType, MemberInfo propertyInfo, string objValue)
  73. {
  74. if(objValue == null)
  75. return null;
  76. if(! (!objType.Equals(typeof(Boolean)) || objValue.Length > 0) )
  77. {
  78. return null;
  79. }
  80. if(objType.IsEnum)
  81. {
  82. return EnumFromString(objType, objValue);
  83. }
  84. if(objType.Equals(typeof(string)))
  85. {
  86. return objValue;
  87. }
  88. PropertyDescriptor pc = null;
  89. if(propertyInfo != null)
  90. {
  91. pc = (TypeDescriptor.GetProperties(propertyInfo.ReflectedType))[propertyInfo.Name];
  92. }
  93. if(pc != null)
  94. {
  95. TypeConverter converter = pc.Converter;
  96. if(converter!=null && converter.CanConvertFrom(typeof(string)))
  97. {
  98. return converter.ConvertFromInvariantString(objValue);
  99. }
  100. }
  101. MethodInfo mi = objType.GetMethod("Parse", parseMethodTypesWithSOP);
  102. object o = null;
  103. if(mi != null)
  104. {
  105. object[] parameters = new object[2];
  106. parameters[0] = objValue;
  107. parameters[1] = CultureInfo.InvariantCulture;
  108. try
  109. {
  110. o = Utils.InvokeMethod(mi, null, parameters);
  111. } catch
  112. {
  113. }
  114. }
  115. if(o == null)
  116. {
  117. mi = objType.GetMethod("Parse", parseMethodTypes);
  118. if(mi!=null)
  119. {
  120. object[] parameters = new object[1];
  121. parameters[0] = objValue;
  122. try
  123. {
  124. o = Utils.InvokeMethod(mi, null, parameters);
  125. } catch
  126. {
  127. }
  128. }
  129. }
  130. if(o == null)
  131. {
  132. throw new HttpException(/*HttpRuntime.FormatResourceString(*/"Type_not_creatable_from_string"/*, objType.FullName, objValue, propertyInfo.Name)*/);
  133. }
  134. return o;
  135. }
  136. }
  137. }