3

System.Linq.ExpressionsWCF経由で送信できるように、DataContractsにシリアル化するクラスをいくつか作成しました。それはかなりうまく機能します。問題は、変数を含む式をシリアル化する場合です。問題を説明する例を次に示します。

public class Foo
{
    public string Name { get; set; }
}

// CASE 1
Expression<Func<Foo, bool>> lambda = foo => foo.Name == "Test";
Console.WriteLine(lambda);
// OUTPUT: foo => (foo.Name == "Test")

// CASE 2
var variable = "Test";
lambda = foo => foo.Name == variable;            
this.AssertExpression(lambda, "Class Lambda expression with variable.");
// OUTPUT: foo => (foo.Name == value(MyTest+<>c__DisplayClass0).variable)

CASE 2式をシリアル化するのに問題はありませんが、サービス側では解決するものがないため、シリアル化するデータは役に立ちません。value(MyTest+<>c__DisplayClass0).variable

したがって、CASE 2式がCASE1と同じ結果にシリアル化されるように、その式をシリアル化する前に変数を解決する必要があります。

4

1 に答える 1

2

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 を使用していない場合は、詳細やコードを教えていただけますか?

幸運を!

于 2011-12-09T16:40:48.823 に答える