我有这样的表达:
Expression<Func<MyEntity, bool>> exp = x => x.FirstName == "Jonas";
该表达式被传输到另一个不具有类型的应用程序MyEntity
。
为了能够执行表达式,我尝试将其中的类型替换为ExpandoObject
使用ExpressionVistor
.
public class ReplaceToExpandoVisitor : ExpressionVisitor
{
ParameterExpression _parameter;
private Type _targetType = typeof(ExpandoObject);
public ReplaceToExpandoVisitor(ParameterExpression p2)
{
_parameter = p2;
}
protected override Expression VisitParameter(ParameterExpression node)
{
return _parameter;
}
protected override Expression VisitMember(MemberExpression node)
{
if (node.Member.MemberType != System.Reflection.MemberTypes.Property)
throw new NotSupportedException();
var memberName = node.Member.Name;
var propBinder = Binder.GetMember(CSharpBinderFlags.None,
memberName,
GetType(),
new[] { CSharpArgumentInfo.Create(CSharpArgumentInfoFlags.None, null) });
var inner = Visit(node.Expression);
// this is the right way, right?
var exp2 = Expression.Dynamic(propBinder, typeof(object), inner);
// I need to convert it right? Otherwise it will be of type object?
var propGetExpression = Expression.Convert(exp2, node.Type);
return propGetExpression;
}
}
然而,该表达式在执行时返回 false。所以我想我没有正确访问 Expandoobject 中的“属性”。
有人可以解释我做错了什么吗?
翻阅古今
相关分类