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)
9 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
11 // Permission is hereby granted, free of charge, to any person obtaining
12 // a copy of this software and associated documentation files (the
13 // "Software"), to deal in the Software without restriction, including
14 // without limitation the rights to use, copy, modify, merge, publish,
15 // distribute, sublicense, and/or sell copies of the Software, and to
16 // permit persons to whom the Software is furnished to do so, subject to
17 // the following conditions:
19 // The above copyright notice and this permission notice shall be
20 // included in all copies or substantial portions of the Software.
22 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
23 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
24 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
25 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
26 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
27 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
28 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 using System.Collections;
32 using System.ComponentModel;
33 using System.Reflection;
34 using System.Security.Permissions;
36 namespace System.Web.UI {
38 // CAS - no InheritanceDemand here as the class is sealed
39 [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
40 public sealed class DataBinder {
45 internal static string FormatResult (object result, string format)
50 if (format == null || format.Length == 0)
51 return result.ToString ();
53 return String.Format (format, result);
56 public static object Eval (object container, string expression)
58 if ((expression == null) || (expression.Length == 0))
59 throw new ArgumentNullException ("expression");
61 object current = container;
63 while (current != null) {
64 int dot = expression.IndexOf ('.');
65 int size = (dot == -1) ? expression.Length : dot;
66 string prop = expression.Substring (0, size);
67 if (prop.IndexOf ('[') != -1)
68 current = GetIndexedPropertyValue (current, prop);
70 current = GetPropertyValue (current, prop);
75 expression = expression.Substring (prop.Length + 1);
81 public static string Eval (object container, string expression, string format)
83 object result = Eval (container, expression);
84 return FormatResult (result, format);
87 public static object GetIndexedPropertyValue (object container, string expr)
89 if (container == null)
90 throw new ArgumentNullException ("container");
91 if ((expr == null) || (expr.Length == 0))
92 throw new ArgumentNullException ("expr");
94 int openIdx = expr.IndexOf ('[');
95 int closeIdx = expr.IndexOf (']'); // see the test case. MS ignores all after the first ]
96 if (openIdx < 0 || closeIdx < 0 || closeIdx - openIdx <= 1)
97 throw new ArgumentException (expr + " is not a valid indexed expression.");
99 string val = expr.Substring (openIdx + 1, closeIdx - openIdx - 1);
102 throw new ArgumentException (expr + " is not a valid indexed expression.");
104 bool is_string = false;
105 // a quoted val means we have a string
106 if ((val[0] == '\'' && val[val.Length - 1] == '\'') ||
107 (val[0] == '\"' && val[val.Length - 1] == '\"')) {
109 val = val.Substring(1, val.Length - 2);
111 // if all chars are digits, then we have a int
112 for(int i = 0; i < val.Length; i++)
113 if (!Char.IsDigit(val[i])) {
122 intVal = Int32.Parse (val);
124 throw new ArgumentException (expr + " is not a valid indexed expression.");
128 string property = null;
130 property = expr.Substring (0, openIdx);
131 if (property != null && property != String.Empty)
132 container = GetPropertyValue (container, property);
135 if (container == null)
138 if (container is System.Collections.IList) {
139 IList l = (IList) container;
143 Type t = container.GetType ();
144 // MS does not seem to look for any other than "Item"!!!
145 object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
146 if (atts.Length != 1)
147 throw new ArgumentException (expr + " indexer not found.");
149 property = ((DefaultMemberAttribute) atts [0]).MemberName;
151 Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
152 PropertyInfo prop = t.GetProperty (property, argTypes);
154 throw new ArgumentException (expr + " indexer not found.");
156 object [] args = new object [1];
162 return prop.GetValue (container, args);
165 public static string GetIndexedPropertyValue (object container, string expr, string format)
167 object result = GetIndexedPropertyValue (container, expr);
168 return FormatResult (result, format);
171 public static object GetPropertyValue (object container, string propName)
173 if (container == null)
174 throw new ArgumentNullException ("container");
175 if (propName == null)
176 throw new ArgumentNullException ("propName");
178 PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
180 throw new HttpException ("Property " + propName + " not found in " +
181 container.GetType ());
184 return prop.GetValue (container);
187 public static string GetPropertyValue (object container, string propName, string format)
189 object result = GetPropertyValue (container, propName);
190 return FormatResult (result, format);
194 public static object GetDataItem (object container, out bool foundDataItem)
196 foundDataItem = false;
197 if (container == null)
200 if (container is IDataItemContainer) {
201 foundDataItem = true;
202 return ((IDataItemContainer)container).DataItem;
205 PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
209 foundDataItem = true;
210 return pi.GetValue (container, null);
214 public static object GetDataItem (object container)
217 return GetDataItem (container, out flag);