DataBinder.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. //
  2. // System.Web.UI.DataBinder.cs
  3. //
  4. // Authors:
  5. // Duncan Mak ([email protected])
  6. // Gonzalo Paniagua Javier ([email protected])
  7. //
  8. // (C) 2002 Ximian, Inc. (http://www.ximian.com)
  9. //
  10. using System;
  11. using System.ComponentModel;
  12. using System.Reflection;
  13. namespace System.Web.UI {
  14. public sealed class DataBinder
  15. {
  16. public DataBinder ()
  17. {
  18. }
  19. private static string FormatResult (object result, string format)
  20. {
  21. if (result == null)
  22. return String.Empty;
  23. if (format == null)
  24. return result.ToString ();
  25. return String.Format (format, result);
  26. }
  27. public static object Eval (object container, string expression)
  28. {
  29. if (expression == null)
  30. throw new ArgumentNullException ("expression");
  31. object current = container;
  32. while (current != null) {
  33. int dot = expression.IndexOf ('.');
  34. int size = (dot == -1) ? expression.Length : dot;
  35. string prop = expression.Substring (0, size);
  36. if (prop.IndexOf ('[') != -1)
  37. current = GetIndexedPropertyValue (current, prop);
  38. else
  39. current = GetPropertyValue (current, prop);
  40. if (dot == -1)
  41. break;
  42. expression = expression.Substring (prop.Length + 1);
  43. }
  44. return current;
  45. }
  46. public static string Eval (object container, string expression, string format)
  47. {
  48. object result = Eval (container, expression);
  49. return FormatResult (result, format);
  50. }
  51. public static object GetIndexedPropertyValue (object container, string expr)
  52. {
  53. if (expr == null)
  54. throw new ArgumentNullException ("expr");
  55. int openIdx = expr.IndexOf ('[');
  56. int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
  57. if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
  58. throw new ArgumentException (expr + " is not a valid indexed expression.");
  59. string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
  60. val = val.Trim ();
  61. int valLength = val.Length;
  62. if (valLength == 0)
  63. throw new ArgumentException (expr + " is not a valid indexed expression.");
  64. int intVal = 0;
  65. bool is_string;
  66. char first = val [0];
  67. if (first >= '0' && first <= '9') {
  68. is_string = false;
  69. try {
  70. intVal = Int32.Parse (val);
  71. } catch {
  72. throw new ArgumentException (expr + " is not a valid indexed expression.");
  73. }
  74. } else if (first == '"' && val [valLength - 1] == '"') {
  75. is_string = true;
  76. val = val.Substring (0, val.Length - 1).Substring (1);
  77. } else {
  78. throw new ArgumentException (expr + " is not a valid indexed expression.");
  79. }
  80. string property = null;
  81. if (openIdx > 0) {
  82. property = expr.Substring (0, openIdx);
  83. if (property != null && property != String.Empty)
  84. container = GetPropertyValue (container, property);
  85. }
  86. if (container == null)
  87. return null;
  88. Type t = container.GetType ();
  89. // MS does not seem to look for any other than "Item"!!!
  90. object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
  91. if (atts.Length != 1)
  92. throw new ArgumentException (expr + " indexer not found.");
  93. property = ((DefaultMemberAttribute) atts [0]).MemberName;
  94. Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
  95. PropertyInfo prop = t.GetProperty (property, argTypes);
  96. if (prop == null)
  97. throw new ArgumentException (expr + " indexer not found.");
  98. object [] args = new object [1];
  99. if (is_string)
  100. args [0] = val;
  101. else
  102. args [0] = intVal;
  103. return prop.GetValue (container, args);
  104. }
  105. public static string GetIndexedPropertyValue (object container, string expr, string format)
  106. {
  107. object result = GetIndexedPropertyValue (container, expr);
  108. return FormatResult (result, format);
  109. }
  110. public static object GetPropertyValue (object container, string propName)
  111. {
  112. if (propName == null)
  113. throw new ArgumentNullException ("propName");
  114. PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
  115. if (prop == null) {
  116. throw new HttpException ("Property " + propName + " not found in " +
  117. container.GetType ());
  118. }
  119. return prop.GetValue (container);
  120. }
  121. public static string GetPropertyValue (object container, string propName, string format)
  122. {
  123. object result = GetPropertyValue (container, propName);
  124. return FormatResult (result, format);
  125. }
  126. }
  127. }