DataBinder.cs 4.4 KB

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