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.Collections;
13 using System.ComponentModel;
14 using System.Reflection;
16 namespace System.Web.UI {
18 public sealed class DataBinder
24 private static string FormatResult (object result, string format)
30 return result.ToString ();
32 return String.Format (format, result);
35 public static object Eval (object container, string expression)
37 if (expression == null)
38 throw new ArgumentNullException ("expression");
40 object current = container;
42 while (current != null) {
43 int dot = expression.IndexOf ('.');
44 int size = (dot == -1) ? expression.Length : dot;
45 string prop = expression.Substring (0, size);
46 if (prop.IndexOf ('[') != -1)
47 current = GetIndexedPropertyValue (current, prop);
49 current = GetPropertyValue (current, prop);
54 expression = expression.Substring (prop.Length + 1);
60 public static string Eval (object container, string expression, string format)
62 object result = Eval (container, expression);
63 return FormatResult (result, format);
66 public static object GetIndexedPropertyValue (object container, string expr)
69 throw new ArgumentNullException ("expr");
71 int openIdx = expr.IndexOf ('[');
72 int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
73 if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
74 throw new ArgumentException (expr + " is not a valid indexed expression.");
76 string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
78 int valLength = val.Length;
80 throw new ArgumentException (expr + " is not a valid indexed expression.");
85 if (first >= '0' && first <= '9') {
88 intVal = Int32.Parse (val);
90 throw new ArgumentException (expr + " is not a valid indexed expression.");
93 } else if (first == '"' && val [valLength - 1] == '"') {
95 val = val.Substring (0, val.Length - 1).Substring (1);
97 throw new ArgumentException (expr + " is not a valid indexed expression.");
100 string property = null;
102 property = expr.Substring (0, openIdx);
103 if (property != null && property != String.Empty)
104 container = GetPropertyValue (container, property);
107 if (container == null)
110 if (container is System.Collections.IList) {
111 IList l = (IList) container;
115 Type t = container.GetType ();
116 // MS does not seem to look for any other than "Item"!!!
117 object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
118 if (atts.Length != 1)
119 throw new ArgumentException (expr + " indexer not found.");
121 property = ((DefaultMemberAttribute) atts [0]).MemberName;
123 Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
124 PropertyInfo prop = t.GetProperty (property, argTypes);
126 throw new ArgumentException (expr + " indexer not found.");
128 object [] args = new object [1];
134 return prop.GetValue (container, args);
137 public static string GetIndexedPropertyValue (object container, string expr, string format)
139 object result = GetIndexedPropertyValue (container, expr);
140 return FormatResult (result, format);
143 public static object GetPropertyValue (object container, string propName)
145 if (propName == null)
146 throw new ArgumentNullException ("propName");
148 PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
150 throw new HttpException ("Property " + propName + " not found in " +
151 container.GetType ());
154 return prop.GetValue (container);
157 public static string GetPropertyValue (object container, string propName, string format)
159 object result = GetPropertyValue (container, propName);
160 return FormatResult (result, format);
164 public static object GetDataItem (object container, out bool foundDataItem)
166 foundDataItem = false;
167 if (container == null)
170 PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
174 foundDataItem = true;
175 return pi.GetValue (container, null);
179 public static object GetDataItem (object container)
182 return GetDataItem (container, out flag);