DataBinder.cs 7.0 KB

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