PropertyConverter.cs 2.9 KB

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