Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6935383
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:05:44+00:00 2026-05-27T12:05:44+00:00

I wrote some classes to serialize System.Linq.Expressions to DataContracts to be able to send

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:05:45+00:00Added an answer on May 27, 2026 at 12:05 pm

    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!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this header file, zeeheader.h, and I wrote some classes in it, I'm
I wrote some code with a lot of recursion, that takes quite a bit
I need to XML serialize some classes, which in some cases do not adhere
I have a complex graph of XML-serializable classes that I'm able to (de)serialize to
There're some library classes that although implement Serializable , fail to serialize correctly. I
I'd like to serialize some LINQ generated objects and store them in a table
I need to write some methods for loading/saving some classes to and from a
I am wondering should I write unit test for everything. There are some classes
I wrote some CSS in my HTML code to create rollover buttons. Then i
I wrote some code recently (ISO/ANSI C), and was surprised at the poor performance

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.