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

  • Home
  • SEARCH
  • 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 6250393
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T13:24:55+00:00 2026-05-24T13:24:55+00:00

I created an ExpressionVisitor implementation that overrides VisitConstant. However, when I create an expression

  • 0

I created an ExpressionVisitor implementation that overrides VisitConstant. However, when I create an expression that utilizes a local variable I can’t seem to get the actual value of the variable.

public class Person
{
  public string FirstName { get; set; }
}

string name = "Michael";

Expression<Func<Person, object>> exp = p => p.FirstName == name;

How in the world do I get the value of the variable “name” from the ConstantExpression?
The only thing that I can think of is this:

string fieldValue = value.GetType().GetFields().First().GetValue(value).ToString();

Obviously this doesn’t lend itself to being very flexible though….

A slightly more complicated example would be the following:

Person localPerson = new Person { FirstName = "Michael" };
Expression<Func<Person, object>> exp = p => p.FirstName == localPerson.FirstName;
  • 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-24T13:24:57+00:00Added an answer on May 24, 2026 at 1:24 pm

    Here’s how I solved it for both cases you listed.

    Basically assuming that the right hand side of your ‘==’ can be treated like a function that takes no arguments and returns a value, it can be compiled to a C# delegate and invoked to retrieve this value without worrying about exactly what the code on the right hand side does.

    So the basic example code is below

    class Visitor : ExpressionVisitor {
    
      protected override Expression VisitBinary( BinaryExpression node ) {
    
        var memberLeft = node.Left as MemberExpression;
        if ( memberLeft != null && memberLeft.Expression is ParameterExpression ) {
    
          var f = Expression.Lambda( node.Right ).Compile();
          var value = f.DynamicInvoke();
          }
    
        return base.VisitBinary( node );
        }
      }
    

    It looks for a binary op looking for “arg.member == something” then just compiles/evaluates the right hand side, and for both examples your provide the result is a string “Michael”.

    Note, this fails if your right hand side involved using the lamda argument like

    p.FirstName == CallSomeFunc( p.FirstName )

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

Sidebar

Related Questions

I created a control where other developers can create an instance and use. There
created an Eclipse plugin that contributes to the UI by a new project wizard
Created a google map with GMap2 and put pinpoints on there that open up
Created a new project from the Tab Bar Application template, how can I access
i created a datalayer using subsonic. however when i am selecting my data, i
Created a WCF service that is being called by a Silverlight app. Both reside
I created an app that allows the user to select a video from the
i created a WCF service that also writes into a log file. i put
I created a simple test that opens a connection of sqlce located on my
Created a simple WCF service that basically logs to a db. the build is

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.