// Gonzalo Paniagua Javier (gonzalo@ximian.com)
//
// (C) 2002 Ximian, Inc. (http://www.ximian.com)
-// Copyright (C) 2005 Novell, Inc (http://www.novell.com)
+// Copyright (C) 2005-2010 Novell, Inc (http://www.novell.com)
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
//
using System.Collections;
+using System.Collections.Generic;
using System.ComponentModel;
using System.Reflection;
using System.Security.Permissions;
if (result == null)
return String.Empty;
- if (format == null)
+ if (format == null || format.Length == 0)
return result.ToString ();
return String.Format (format, result);
public static object Eval (object container, string expression)
{
- if ((expression == null) || (expression.Length == 0))
+ expression = expression != null ? expression.Trim () : null;
+ if (expression == null || expression.Length == 0)
throw new ArgumentNullException ("expression");
object current = container;
-
while (current != null) {
int dot = expression.IndexOf ('.');
int size = (dot == -1) ? expression.Length : dot;
string prop = expression.Substring (0, size);
+
if (prop.IndexOf ('[') != -1)
current = GetIndexedPropertyValue (current, prop);
else
current = GetPropertyValue (current, prop);
-
+
if (dot == -1)
break;
string property = null;
if (openIdx > 0) {
property = expr.Substring (0, openIdx);
- if (property != null && property != String.Empty)
+ if (property != null && property.Length > 0)
container = GetPropertyValue (container, property);
}
return null;
if (container is System.Collections.IList) {
+ if (is_string)
+ throw new ArgumentException (expr + " cannot be indexed with a string.");
IList l = (IList) container;
return l [intVal];
}
Type t = container.GetType ();
+
// MS does not seem to look for any other than "Item"!!!
object [] atts = t.GetCustomAttributes (typeof (DefaultMemberAttribute), false);
if (atts.Length != 1)
- throw new ArgumentException (expr + " indexer not found.");
-
- property = ((DefaultMemberAttribute) atts [0]).MemberName;
+ property = "Item";
+ else
+ property = ((DefaultMemberAttribute) atts [0]).MemberName;
Type [] argTypes = new Type [] { (is_string) ? typeof (string) : typeof (int) };
PropertyInfo prop = t.GetProperty (property, argTypes);
{
if (container == null)
throw new ArgumentNullException ("container");
- if (propName == null)
+ if (propName == null || propName.Length == 0)
throw new ArgumentNullException ("propName");
PropertyDescriptor prop = TypeDescriptor.GetProperties (container).Find (propName, true);
return FormatResult (result, format);
}
- #if NET_2_0
+ [ThreadStatic]
+ static Dictionary<Type, PropertyInfo> dataItemCache;
+
public static object GetDataItem (object container, out bool foundDataItem)
{
foundDataItem = false;
return ((IDataItemContainer)container).DataItem;
}
- PropertyInfo pi = container.GetType ().GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
+ PropertyInfo pi = null;
+ if (dataItemCache == null)
+ dataItemCache = new Dictionary<Type, PropertyInfo> ();
+
+ Type type = container.GetType ();
+ if (!dataItemCache.TryGetValue (type, out pi)) {
+ pi = type.GetProperty ("DataItem", BindingFlags.Public | BindingFlags.Instance);
+ dataItemCache [type] = pi;
+ }
+
if (pi == null)
return null;
foundDataItem = true;
+
return pi.GetValue (container, null);
}
bool flag;
return GetDataItem (container, out flag);
}
- #endif
}
}