DataBinder.cs 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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.Collections;
  12. using System.ComponentModel;
  13. using System.Reflection;
  14. namespace System.Web.UI {
  15. public sealed class DataBinder
  16. {
  17. public DataBinder ()
  18. {
  19. }
  20. private static string FormatResult (object result, string format)
  21. {
  22. if (result == null)
  23. return String.Empty;
  24. if (format == null)
  25. return result.ToString ();
  26. return String.Format (format, result);
  27. }
  28. public static object Eval (object container, string expression)
  29. {
  30. if (expression == null)
  31. throw new ArgumentNullException ("expression");
  32. object current = container;
  33. while (current != null) {
  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 (expr == null)
  55. throw new ArgumentNullException ("expr");
  56. int openIdx = expr.IndexOf ('[');
  57. int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
  58. if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
  59. throw new ArgumentException (expr + " is not a valid indexed expression.");
  60. string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
  61. val = val.Trim ();
  62. int valLength = val.Length;
  63. if (valLength == 0)
  64. throw new ArgumentException (expr + " is not a valid indexed expression.");
  65. int intVal = 0;
  66. bool is_string;
  67. char first = val [0];
  68. if (first >= '0' && first <= '9') {
  69. is_string = false;
  70. try {
  71. intVal = Int32.Parse (val);
  72. } catch {
  73. throw new ArgumentException (expr + " is not a valid indexed expression.");
  74. }
  75. } else if (first == '"' && val [valLength - 1] == '"') {
  76. is_string = true;
  77. val = val.Substring (0, val.Length - 1).Substring (1);
  78. } else {
  79. throw new ArgumentException (expr + " is not a valid indexed expression.");
  80. }
  81. string property = null;
  82. if (openIdx > 0) {
  83. property = expr.Substring (0, openIdx);
  84. if (property != null && property != String.Empty)
  85. container = GetPropertyValue (container, property);
  86. }
  87. if (container == null)
  88. return null;
  89. if (container is System.Collections.IList) {
  90. IList l = (IList) container;
  91. return l [intVal];
  92. }
  93. Type t = container.GetType ();
  94. // MS does not seem to look for any other than "Item"!!!
  95. object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
  96. if (atts.Length != 1)
  97. throw new ArgumentException (expr + " indexer not found.");
  98. property = ((DefaultMemberAttribute) atts [0]).MemberName;
  99. Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
  100. PropertyInfo prop = t.GetProperty (property, argTypes);
  101. if (prop == null)
  102. throw new ArgumentException (expr + " indexer not found.");
  103. object [] args = new object [1];
  104. if (is_string)
  105. args [0] = val;
  106. else
  107. args [0] = intVal;
  108. return prop.GetValue (container, args);
  109. }
  110. public static string GetIndexedPropertyValue (object container, string expr, string format)
  111. {
  112. object result = GetIndexedPropertyValue (container, expr);
  113. return FormatResult (result, format);
  114. }
  115. public static object GetPropertyValue (object container, string propName)
  116. {
  117. if (propName == null)
  118. throw new ArgumentNullException ("propName");
  119. PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
  120. if (prop == null) {
  121. throw new HttpException ("Property " + propName + " not found in " +
  122. container.GetType ());
  123. }
  124. return prop.GetValue (container);
  125. }
  126. public static string GetPropertyValue (object container, string propName, string format)
  127. {
  128. object result = GetPropertyValue (container, propName);
  129. return FormatResult (result, format);
  130. }
  131. #if NET_1_2
  132. public static object GetDataItem (object container, out bool foundDataItem)
  133. {
  134. foundDataItem = false;
  135. if (container == null)
  136. return null;
  137. PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
  138. if (pi == null)
  139. return null;
  140. foundDataItem = true;
  141. return pi.GetValue (container, null);
  142. }
  143. public static object GetDataItem (object container)
  144. {
  145. bool flag;
  146. return GetDataItem (container, out flag);
  147. }
  148. #endif
  149. }
  150. }