DataBinder.cs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System;
  31. using System.Collections;
  32. using System.ComponentModel;
  33. using System.Reflection;
  34. namespace System.Web.UI {
  35. public sealed class DataBinder
  36. {
  37. public DataBinder ()
  38. {
  39. }
  40. private static string FormatResult (object result, string format)
  41. {
  42. if (result == null)
  43. return String.Empty;
  44. if (format == null)
  45. return result.ToString ();
  46. return String.Format (format, result);
  47. }
  48. public static object Eval (object container, string expression)
  49. {
  50. if (expression == null)
  51. throw new ArgumentNullException ("expression");
  52. object current = container;
  53. while (current != null) {
  54. int dot = expression.IndexOf ('.');
  55. int size = (dot == -1) ? expression.Length : dot;
  56. string prop = expression.Substring (0, size);
  57. if (prop.IndexOf ('[') != -1)
  58. current = GetIndexedPropertyValue (current, prop);
  59. else
  60. current = GetPropertyValue (current, prop);
  61. if (dot == -1)
  62. break;
  63. expression = expression.Substring (prop.Length + 1);
  64. }
  65. return current;
  66. }
  67. public static string Eval (object container, string expression, string format)
  68. {
  69. object result = Eval (container, expression);
  70. return FormatResult (result, format);
  71. }
  72. public static object GetIndexedPropertyValue (object container, string expr)
  73. {
  74. if (expr == null)
  75. throw new ArgumentNullException ("expr");
  76. int openIdx = expr.IndexOf ('[');
  77. int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
  78. if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
  79. throw new ArgumentException (expr + " is not a valid indexed expression.");
  80. string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
  81. val = val.Trim ();
  82. if (val.Length == 0)
  83. throw new ArgumentException (expr + " is not a valid indexed expression.");
  84. bool is_string = false;
  85. // a quoted val means we have a string
  86. if ((val[0] == '\'' && val[val.Length - 1] == '\'') ||
  87. (val[0] == '\"' && val[val.Length - 1] == '\"')) {
  88. is_string = true;
  89. val = val.Substring(1, val.Length - 2);
  90. } else {
  91. // if all chars are digits, then we have a int
  92. for(int i = 0; i < val.Length; i++)
  93. if (!Char.IsDigit(val[i])) {
  94. is_string = true;
  95. break;
  96. }
  97. }
  98. int intVal = 0;
  99. if (!is_string) {
  100. try {
  101. intVal = Int32.Parse (val);
  102. } catch {
  103. throw new ArgumentException (expr + " is not a valid indexed expression.");
  104. }
  105. }
  106. string property = null;
  107. if (openIdx > 0) {
  108. property = expr.Substring (0, openIdx);
  109. if (property != null && property != String.Empty)
  110. container = GetPropertyValue (container, property);
  111. }
  112. if (container == null)
  113. return null;
  114. if (container is System.Collections.IList) {
  115. IList l = (IList) container;
  116. return l [intVal];
  117. }
  118. Type t = container.GetType ();
  119. // MS does not seem to look for any other than "Item"!!!
  120. object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
  121. if (atts.Length != 1)
  122. throw new ArgumentException (expr + " indexer not found.");
  123. property = ((DefaultMemberAttribute) atts [0]).MemberName;
  124. Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
  125. PropertyInfo prop = t.GetProperty (property, argTypes);
  126. if (prop == null)
  127. throw new ArgumentException (expr + " indexer not found.");
  128. object [] args = new object [1];
  129. if (is_string)
  130. args [0] = val;
  131. else
  132. args [0] = intVal;
  133. return prop.GetValue (container, args);
  134. }
  135. public static string GetIndexedPropertyValue (object container, string expr, string format)
  136. {
  137. object result = GetIndexedPropertyValue (container, expr);
  138. return FormatResult (result, format);
  139. }
  140. public static object GetPropertyValue (object container, string propName)
  141. {
  142. if (propName == null)
  143. throw new ArgumentNullException ("propName");
  144. PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
  145. if (prop == null) {
  146. throw new HttpException ("Property " + propName + " not found in " +
  147. container.GetType ());
  148. }
  149. return prop.GetValue (container);
  150. }
  151. public static string GetPropertyValue (object container, string propName, string format)
  152. {
  153. object result = GetPropertyValue (container, propName);
  154. return FormatResult (result, format);
  155. }
  156. #if NET_2_0
  157. public static object GetDataItem (object container, out bool foundDataItem)
  158. {
  159. foundDataItem = false;
  160. if (container == null)
  161. return null;
  162. PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
  163. if (pi == null)
  164. return null;
  165. foundDataItem = true;
  166. return pi.GetValue (container, null);
  167. }
  168. public static object GetDataItem (object container)
  169. {
  170. bool flag;
  171. return GetDataItem (container, out flag);
  172. }
  173. #endif
  174. }
  175. }