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.Reflection;
14 namespace System.Web.UI {
16 public sealed class DataBinder
22 private static string FormatResult (object result, string format)
28 return result.ToString ();
30 return String.Format (format, result);
33 public static object Eval (object container, string expression)
35 if (container == null)
36 throw new ArgumentNullException ("container");
38 if (expression == null)
39 throw new ArgumentNullException ("expression");
41 object current = container;
44 int dot = expression.IndexOf ('.');
45 int size = (dot == -1) ? expression.Length : dot;
46 string prop = expression.Substring (0, size);
47 if (prop.IndexOf ('[') != -1)
48 current = GetIndexedPropertyValue (current, prop);
50 current = GetPropertyValue (current, prop);
55 expression = expression.Substring (prop.Length + 1);
61 public static string Eval (object container, string expression, string format)
63 object result = Eval (container, expression);
64 return FormatResult (result, format);
67 public static object GetIndexedPropertyValue (object container, string expr)
69 if (container == null)
70 throw new ArgumentNullException ("container");
73 throw new ArgumentNullException ("expr");
75 int openIdx = expr.IndexOf ('[');
76 int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
77 if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
78 throw new ArgumentException (expr + " is not a valid indexed expression.");
80 string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
82 int valLength = val.Length;
84 throw new ArgumentException (expr + " is not a valid indexed expression.");
89 if (first >= '0' && first <= '9') {
92 intVal = Int32.Parse (val);
94 throw new ArgumentException (expr + " is not a valid indexed expression.");
97 } else if (first == '"' && val [valLength - 1] == '"') {
99 val = val.Substring (0, val.Length - 1).Substring (1);
101 throw new ArgumentException (expr + " is not a valid indexed expression.");
104 string property = null;
106 property = expr.Substring (0, openIdx);
107 if (property != null && property != String.Empty)
108 container = DataBinder.GetPropertyValue (container, property);
111 Type t = container.GetType ();
112 // MS does not seem to look for any other than "Item"!!!
113 object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
114 if (atts.Length != 1)
115 throw new ArgumentException (expr + " indexer not found.");
117 property = ((DefaultMemberAttribute) atts [0]).MemberName;
119 Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
120 PropertyInfo prop = t.GetProperty (property, argTypes);
122 throw new ArgumentException (expr + " indexer not found.");
124 object [] args = new object [1];
130 return prop.GetValue (container, args);
133 public static string GetIndexedPropertyValue (object container, string expr, string format)
135 object result = GetIndexedPropertyValue (container, expr);
136 return FormatResult (result, format);
139 public static object GetPropertyValue (object container, string propName)
141 if (container == null || propName == null)
142 throw new ArgumentException ();
144 Type type = container.GetType ();
145 PropertyInfo prop = type.GetProperty (propName);
148 return GetIndexedPropertyValue (container, "[\"" + propName + "\"]");
150 throw new HttpException ("Property " + propName + " not found in " +
156 MethodInfo getm = prop.GetGetMethod ();
158 throw new HttpException ("Cannot find get accessor for " + propName +
159 " in " + type.ToString ());
161 return getm.Invoke (container, null);
164 public static string GetPropertyValue (object container, string propName, string format)
166 object result = GetPropertyValue (container, propName);
167 return FormatResult (result, format);