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);
79 throw new ArgumentException (expr + " is not a valid indexed expression.");
81 bool is_string = false;
82 // a quoted val means we have a string
83 if ((val[0] == '\'' && val[val.Length - 1] == '\'') ||
84 (val[0] == '\"' && val[val.Length - 1] == '\"')) {
86 val = val.Substring(1, val.Length - 2);
88 // if all chars are digits, then we have a int
89 for(int i = 0; i < val.Length; i++)
90 if (!Char.IsDigit(val[i])) {
99 intVal = Int32.Parse (val);
101 throw new ArgumentException (expr + " is not a valid indexed expression.");
105 string property = null;
107 property = expr.Substring (0, openIdx);
108 if (property != null && property != String.Empty)
109 container = GetPropertyValue (container, property);
112 if (container == null)
115 if (container is System.Collections.IList) {
116 IList l = (IList) container;
120 Type t = container.GetType ();
121 // MS does not seem to look for any other than "Item"!!!
122 object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
123 if (atts.Length != 1)
124 throw new ArgumentException (expr + " indexer not found.");
126 property = ((DefaultMemberAttribute) atts [0]).MemberName;
128 Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
129 PropertyInfo prop = t.GetProperty (property, argTypes);
131 throw new ArgumentException (expr + " indexer not found.");
133 object [] args = new object [1];
139 return prop.GetValue (container, args);
142 public static string GetIndexedPropertyValue (object container, string expr, string format)
144 object result = GetIndexedPropertyValue (container, expr);
145 return FormatResult (result, format);
148 public static object GetPropertyValue (object container, string propName)
150 if (propName == null)
151 throw new ArgumentNullException ("propName");
153 PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
155 throw new HttpException ("Property " + propName + " not found in " +
156 container.GetType ());
159 return prop.GetValue (container);
162 public static string GetPropertyValue (object container, string propName, string format)
164 object result = GetPropertyValue (container, propName);
165 return FormatResult (result, format);
169 public static object GetDataItem (object container, out bool foundDataItem)
171 foundDataItem = false;
172 if (container == null)
175 PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
179 foundDataItem = true;
180 return pi.GetValue (container, null);
184 public static object GetDataItem (object container)
187 return GetDataItem (container, out flag);