esskar
esskar

Reputation: 10970

Serialize an expression with an variable

I wrote some classes to serialize System.Linq.Expressions to DataContracts to be able to send via WCF. It works quite good nice. the problem is when i want to serialize an expression that has a variable in it. here is an example to explain the problem:

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)

i am not having trouble to serialize the CASE 2 expression, but the the data i serialize is useless, since on the service side, there is nothing to resolve value(MyTest+<>c__DisplayClass0).variable

so i need to resolve the variables before i serialize that expression so that the CASE 2 expression serializes to same result as CASE1

Upvotes: 3

Views: 1119

Answers (1)

Smudge202
Smudge202

Reputation: 4687

Sorry for the VB, but the following extract is the bit of code I meant in my comment. I don't think it covers all the bases (i.e. it may not be drilling down far enough so make sure you test it) but for simple most examples it works:

The code is based on this MSDN Expression Visitor example:

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);
        }
    }
}   

Hope this helps!

EDIT: The original issue I had was that I didn't continue walking the tree when the MemberAccess was a non TSource class, the above logic should actually recursively root those cases out so ignore my original comment. I've left in the Nullable<T> clause (on the else if) statement as I don't think the existing logic will cover those cases, it may also struggle with Generic classes.

That said, this should put you in good stead. If you're not using the Expression Visitor, can you provide some more details/code?

Good luck!

Upvotes: 2

Related Questions