2 // System.Web.UI.DataBinder.cs
5 // Duncan Mak (duncan@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (C) 2002 Ximian, Inc. (http://www.ximian.com)
12 using System.ComponentModel;
13 using System.Reflection;
15 namespace System.Web.UI {
17 public sealed class DataBinder
23 private static string FormatResult (object result, string format)
29 return result.ToString ();
31 return String.Format (format, result);
34 public static object Eval (object container, string expression)
36 if (expression == null)
37 throw new ArgumentNullException ("expression");
39 object current = container;
41 while (current != null) {
42 int dot = expression.IndexOf ('.');
43 int size = (dot == -1) ? expression.Length : dot;
44 string prop = expression.Substring (0, size);
45 if (prop.IndexOf ('[') != -1)
46 current = GetIndexedPropertyValue (current, prop);
48 current = GetPropertyValue (current, prop);
53 expression = expression.Substring (prop.Length + 1);
59 public static string Eval (object container, string expression, string format)
61 object result = Eval (container, expression);
62 return FormatResult (result, format);
65 public static object GetIndexedPropertyValue (object container, string expr)
68 throw new ArgumentNullException ("expr");
70 int openIdx = expr.IndexOf ('[');
71 int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
72 if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
73 throw new ArgumentException (expr + " is not a valid indexed expression.");
75 string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
77 int valLength = val.Length;
79 throw new ArgumentException (expr + " is not a valid indexed expression.");
84 if (first >= '0' && first <= '9') {
87 intVal = Int32.Parse (val);
89 throw new ArgumentException (expr + " is not a valid indexed expression.");
92 } else if (first == '"' && val [valLength - 1] == '"') {
94 val = val.Substring (0, val.Length - 1).Substring (1);
96 throw new ArgumentException (expr + " is not a valid indexed expression.");
99 string property = null;
101 property = expr.Substring (0, openIdx);
102 if (property != null && property != String.Empty)
103 container = GetPropertyValue (container, property);
106 if (container == null)
109 Type t = container.GetType ();
110 // MS does not seem to look for any other than "Item"!!!
111 object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
112 if (atts.Length != 1)
113 throw new ArgumentException (expr + " indexer not found.");
115 property = ((DefaultMemberAttribute) atts [0]).MemberName;
117 Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
118 PropertyInfo prop = t.GetProperty (property, argTypes);
120 throw new ArgumentException (expr + " indexer not found.");
122 object [] args = new object [1];
128 return prop.GetValue (container, args);
131 public static string GetIndexedPropertyValue (object container, string expr, string format)
133 object result = GetIndexedPropertyValue (container, expr);
134 return FormatResult (result, format);
137 public static object GetPropertyValue (object container, string propName)
139 if (propName == null)
140 throw new ArgumentNullException ("propName");
142 PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
144 throw new HttpException ("Property " + propName + " not found in " +
145 container.GetType ());
148 return prop.GetValue (container);
151 public static string GetPropertyValue (object container, string propName, string format)
153 object result = GetPropertyValue (container, propName);
154 return FormatResult (result, format);
158 public static object GetDataItem (object container, out bool foundDataItem)
160 foundDataItem = false;
161 if (container == null)
164 PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
168 foundDataItem = true;
169 return pi.GetValue (container, null);
173 public static object GetDataItem (object container)
176 return GetDataItem (container, out flag);