private static System.Func<object, object> FindMember(Type type, string name)
{
if (type == null)
throw new ArgumentNullException("type");
if (name == null)
throw new ArgumentNullException("name");
lock (_memberAccessors)
{
Dictionary<string, System.Func<object, object>> members;
System.Func<object, object> accessor = null;
if (_memberAccessors.TryGetValue(type, out members))
{
if (members.TryGetValue(name, out accessor))
return accessor;
}
else
{
members = new Dictionary<string, System.Func<object, object>>();
_memberAccessors[type] = members;
}
// must look up using reflection
string methodSuffix = char.ToUpperInvariant(name[0]) + name.Substring(1);
bool checkOriginalName = !string.Equals(methodSuffix, name);
MethodInfo method = null;
if (method == null)
{
PropertyInfo p = type.GetProperty(methodSuffix);
if (p == null && checkOriginalName)
p = type.GetProperty(name);
if (p != null)
method = p.GetGetMethod();
}
if (method == null)
{
method = type.GetMethod("Get" + methodSuffix, Type.EmptyTypes);
if (method == null && checkOriginalName)
method = type.GetMethod("Get" + name, Type.EmptyTypes);
}
if (method == null)
{
method = type.GetMethod("get_" + methodSuffix, Type.EmptyTypes);
if (method == null && checkOriginalName)
method = type.GetMethod("get_" + name, Type.EmptyTypes);
}
if (method != null)
{
accessor = BuildAccessor(method);
}
else
{
// try for an indexer
method = type.GetMethod("get_Item", new Type[] { typeof(string) });
if (method == null)
{
var property = type.GetProperties().FirstOrDefault(IsIndexer);
if (property != null)
method = property.GetGetMethod();
}
if (method != null)
{
accessor = BuildAccessor(method, name);
}
else
{
// try for a visible field
FieldInfo field = type.GetField(name);
// also check .NET naming convention for fields
if (field == null)
field = type.GetField("_" + name);
if (field != null)
accessor = BuildAccessor(field);
}
}
members[name] = accessor;
return accessor;
}
}