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
Sorry for the VB, butthe 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 forsimplemost examples it works:The code is based on this MSDN Expression Visitor example:
Hope this helps!
EDIT: The original issue I had was that I didn’t continue walking the tree when the MemberAccess was a non
TSourceclass, the above logic should actually recursively root those cases out so ignore my original comment. I’ve left in theNullable<T>clause (on theelse 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!