DataBinder.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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. // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
  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.Collections;
  31. using System.ComponentModel;
  32. using System.Reflection;
  33. using System.Security.Permissions;
  34. namespace System.Web.UI {
  35. // CAS - no InheritanceDemand here as the class is sealed
  36. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  37. public sealed class DataBinder {
  38. public DataBinder ()
  39. {
  40. }
  41. internal static string FormatResult (object result, string format)
  42. {
  43. if (result == null)
  44. return String.Empty;
  45. if (format == null)
  46. return result.ToString ();
  47. return String.Format (format, result);
  48. }
  49. public static object Eval (object container, string expression)
  50. {
  51. if ((expression == null) || (expression.Length == 0))
  52. throw new ArgumentNullException ("expression");
  53. object current = container;
  54. while (current != null) {
  55. int dot = expression.IndexOf ('.');
  56. int size = (dot == -1) ? expression.Length : dot;
  57. string prop = expression.Substring (0, size);
  58. if (prop.IndexOf ('[') != -1)
  59. current = GetIndexedPropertyValue (current, prop);
  60. else
  61. current = GetPropertyValue (current, prop);
  62. if (dot == -1)
  63. break;
  64. expression = expression.Substring (prop.Length + 1);
  65. }
  66. return current;
  67. }
  68. public static string Eval (object container, string expression, string format)
  69. {
  70. object result = Eval (container, expression);
  71. return FormatResult (result, format);
  72. }
  73. public static object GetIndexedPropertyValue (object container, string expr)
  74. {
  75. if (container == null)
  76. throw new ArgumentNullException ("container");
  77. if ((expr == null) || (expr.Length == 0))
  78. throw new ArgumentNullException ("expr");
  79. int openIdx = expr.IndexOf ('[');
  80. int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
  81. if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
  82. throw new ArgumentException (expr + " is not a valid indexed expression.");
  83. string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
  84. val = val.Trim ();
  85. if (val.Length == 0)
  86. throw new ArgumentException (expr + " is not a valid indexed expression.");
  87. bool is_string = false;
  88. // a quoted val means we have a string
  89. if ((val[0] == '\'' && val[val.Length - 1] == '\'') ||
  90. (val[0] == '\"' && val[val.Length - 1] == '\"')) {
  91. is_string = true;
  92. val = val.Substring(1, val.Length - 2);
  93. } else {
  94. // if all chars are digits, then we have a int
  95. for(int i = 0; i < val.Length; i++)
  96. if (!Char.IsDigit(val[i])) {
  97. is_string = true;
  98. break;
  99. }
  100. }
  101. int intVal = 0;
  102. if (!is_string) {
  103. try {
  104. intVal = Int32.Parse (val);
  105. } catch {
  106. throw new ArgumentException (expr + " is not a valid indexed expression.");
  107. }
  108. }
  109. string property = null;
  110. if (openIdx > 0) {
  111. property = expr.Substring (0, openIdx);
  112. if (property != null && property != String.Empty)
  113. container = GetPropertyValue (container, property);
  114. }
  115. if (container == null)
  116. return null;
  117. if (container is System.Collections.IList) {
  118. IList l = (IList) container;
  119. return l [intVal];
  120. }
  121. Type t = container.GetType ();
  122. // MS does not seem to look for any other than "Item"!!!
  123. object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
  124. if (atts.Length != 1)
  125. throw new ArgumentException (expr + " indexer not found.");
  126. property = ((DefaultMemberAttribute) atts [0]).MemberName;
  127. Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
  128. PropertyInfo prop = t.GetProperty (property, argTypes);
  129. if (prop == null)
  130. throw new ArgumentException (expr + " indexer not found.");
  131. object [] args = new object [1];
  132. if (is_string)
  133. args [0] = val;
  134. else
  135. args [0] = intVal;
  136. return prop.GetValue (container, args);
  137. }
  138. public static string GetIndexedPropertyValue (object container, string expr, string format)
  139. {
  140. object result = GetIndexedPropertyValue (container, expr);
  141. return FormatResult (result, format);
  142. }
  143. public static object GetPropertyValue (object container, string propName)
  144. {
  145. if (container == null)
  146. throw new ArgumentNullException ("container");
  147. if (propName == null)
  148. throw new ArgumentNullException ("propName");
  149. PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
  150. if (prop == null) {
  151. throw new HttpException ("Property " + propName + " not found in " +
  152. container.GetType ());
  153. }
  154. return prop.GetValue (container);
  155. }
  156. public static string GetPropertyValue (object container, string propName, string format)
  157. {
  158. object result = GetPropertyValue (container, propName);
  159. return FormatResult (result, format);
  160. }
  161. #if NET_2_0
  162. public static object GetDataItem (object container, out bool foundDataItem)
  163. {
  164. foundDataItem = false;
  165. if (container == null)
  166. return null;
  167. if (container is IDataItemContainer) {
  168. foundDataItem = true;
  169. return ((IDataItemContainer)container).DataItem;
  170. }
  171. PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
  172. if (pi == null)
  173. return null;
  174. foundDataItem = true;
  175. return pi.GetValue (container, null);
  176. }
  177. public static object GetDataItem (object container)
  178. {
  179. bool flag;
  180. return GetDataItem (container, out flag);
  181. }
  182. #endif
  183. }
  184. }