DataBinder.cs 4.6 KB

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