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 7502283
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T20:48:44+00:00 2026-05-29T20:48:44+00:00

I’m trying and failing to use an ExpressionVisitor to modify an expression that calls

  • 0

I’m trying and failing to use an ExpressionVisitor to modify an expression that calls a method. I have a SearchService that encapsulates the search logic and want to be able to amend the search arguments passed.

The class in which the SearchFunc should be modified and run:

  public class SearchService
  {
    public Expression<Func<string, string, List<int>>> SearchFunc { get; set; }

    public void Run()
    {
      SearchModifier modifier = new SearchModifier();
      Expression<Func<string, string, List<int>>> newFunc = (Expression<Func<string, string, List<int>>>)modifier.Modify(SearchFunc);
    }
  }

SearchModifier is defined as:

      public class SearchModifier : ExpressionVisitor
  {
    public Expression Modify(Expression expression)
    {
      return Visit(expression);
    }

    protected override Expression VisitMethodCall(MethodCallExpression node)
    {
      Debug.Print(string.Format("VisitMethodCall: {0}", node.ToString()));
      //VisitMethodCall: value(ExpressionTree_test.MainWindow)._adminRepository.SearchUsers("orig val", "orig val2")

      //trying to use the Update method to create an amended MethodCallExpression
      List<ConstantExpression> newargs = new List<ConstantExpression>();      
      newargs.Add(Expression.Constant("my new arg 1", typeof(string)));
      newargs.Add(Expression.Constant("my new arg 2", typeof(string)));

      MethodCallExpression methodCallExpression = node.Update(node, newargs);
      //causes exception
      //Method 'System.Collections.Generic.List`1[System.Int32] SearchUsers(System.String, System.String)' declared 
      //on type 'ExpressionTree_test.AdminRepository' cannot be called 
      //with instance of type 'System.Collections.Generic.List`1[System.Int32]'

      Debug.Print(string.Format("Amended VisitMethodCall: {0}", methodCallExpression.ToString()));

      return base.VisitMethodCall(node);
    }

The Run method is called like this:

_searchService = new SearchService();
      _searchService.SearchFunc = (t, s) => _adminRepository.SearchUsers("orig val", "orig val2");

I can’t find much information on using the MethodCallExpression.Update method so am not sure I’m doing this correctly. How to I change the values of the arguments in the method?
Of course there may be a better way of doing this and any suggestions gratefully received…

  • 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-29T20:48:48+00:00Added an answer on May 29, 2026 at 8:48 pm

    You’re not using the result of the Update method. You should pass it to base.VisitMethodCall instead of node:

    return base.VisitMethodCall(methodCallExpression);
    

    EDIT

    Sorry, I misread the question… The first argument to Update is not the expression node being visited, it’s the instance on which the method is called. So the code should be:

    node.Update(node.Object, newargs);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
I'm trying to create an if statement in PHP that prevents a single post
I am trying to loop through a bunch of documents I have to put
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

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.