1

Resolved

BuildMemberExpression doesn't work with interfaces

description

The BuildMemberExpression method doesn't work with interfaces. I did already point out the problem on your blog, alog with a working solution. Here it is:
 
private static Expression BuildMemberExpression(Expression exp, IList<string> propertyList) {
if (propertyList.Count == 0)
return exp;
 
List<Type> allTypes = new List<Type>();
allTypes.Add(exp.Type);
allTypes.AddRange(exp.Type.GetInterfaces());
 
MemberExpression newExp = null;
foreach (Type item in allTypes)
{
try
{
newExp = Expression.Property(Expression.TypeAs(exp, item), propertyList[0]);
break;
}
catch
{
continue;
}
}
if (newExp == null) throw new ArgumentException();
 
propertyList.RemoveAt(0);
 
return BuildMemberExpression(newExp, propertyList);
}

comments