VB で申し訳ありませんが、次の抜粋は、コメントで意図したコードの一部です。すべてのベースをカバーしているとは思いません(つまり、十分にドリルダウンしていない可能性があるため、必ずテストしてください)が、単純なほとんどの例では機能します。
コードは、この MSDN Expression Visitor の例に基づいています。
class CustomExpressionWalker<TSource> : ExpressionVisitor
{
protected override Expression VisitMemberAccess(MemberExpression m)
{
if (m.Member.DeclaringType != typeof(TSource))
{
// We are accessing a member/variable on a class
// We need to descend the tree through the navigation properties and eventually derive a constant expression
return this.VisitMemberAccess(m, m.Type);
}
throw new NotImplementedException();
}
protected Expression VisitMemberAccess(MemberExpression m, Type expectedType)
{
if (m.Expression.NodeType == ExpressionType.Constant)
{
// We are at the end of the member expression
// i.e. MyClass.Something.Something.Value <-- we're at the Value part now
ConstantExpression constant = (ConstantExpression)m.Expression;
return Expression.Constant(m.Expression.Type.GetFields().Single(n => n.FieldType == expectedType && m.Member.Name.Contains(n.Name)).GetValue(constant.Value));
}
else if (m.Member.DeclaringType == typeof(TSource))
{
// I'm unsure of your current implementation but the original Member access
// regarding serializing the expression, but if the code reaches here a nested
// MemberAccess has landed on a Property/variable of type TSource, so you'll need
// to decide whether to serialize here or not. For example, if TSource was of
// type "myClass", it could be
// (myOtherClass x) => x.myClass
throw new NotImplementedException();
}
else if (m.Member.DeclaringType == typeof(Nullable))
{
// never got round to implementing this as we don't need it yet
// if you want to deal with Nullable<T> you're going to have to
// examine the logic here
throw new NotImplementedException();
}
else
{
// continue walking the member access until we derive the constant
return this.VisitMemberAccess((MemberExpression)m.Expression, expectedType);
}
}
}
お役に立てれば!
編集:私が抱えていた最初の問題は、MemberAccess が非クラスのときにツリーを歩き続けなかったことですTSource
。上記のロジックは、実際にはこれらのケースを再帰的に根絶する必要があるため、元のコメントは無視してください。既存のロジックがこれらのケースをカバーするとは思わないため、Nullable<T>
句( else if
)ステートメントに残しました。ジェネリック クラスでも苦労する可能性があります。
とはいえ、これはあなたを良い立場に置くはずです。Expression Visitor を使用していない場合は、詳細やコードを教えていただけますか?
幸運を!