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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:40:35+00:00 2026-05-26T07:40:35+00:00

Is it possible to write code like the following. I’m trying to using Moq

  • 0

Is it possible to write code like the following. I’m trying to using Moq with objects that I’m reflecting on as part of a testing framework. The code below raises a "Unhandled expression type: ‘Goto’" exception from Moq, which I guess is expecting something different. It kind of looks like it should work though!

    private void button1_Click(object sender, EventArgs e)
    {
        Ifoo  = foo Foo();

        // Create input parameter for lambda
        ParameterExpression value = Expression.Parameter(typeof(IFoo), "value");

        // create return statement for lambda
        Expression setupProperty = Expression.Return(Expression.Label(), Expression.Property(value, "Bar"), typeof(string));

        // convert expression to lambda (should now be the equivalent of "v => v.Bar")
        var func = Expression.Lambda<Func<IFoo, string>>(setupProperty, value);//.Compile();
        //string s = func(foo); // this bit works fine if .Compile() is included

        var mockFoo = new Mock<IFoo>();

        mockFoo.SetupProperty(func); // exception thrown by moq here, obviously isn't exactly the same as "v => v.Bar"
        mockFoo.Object.Bar = "Burge+";
    }

Thanks!

  • 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-26T07:40:36+00:00Added an answer on May 26, 2026 at 7:40 am

    Ok, this is possible, here is the corrected code.

    // Create input parameter for lambda 
    ParameterExpression value = Expression.Parameter(typeof(IFoo), "value"); 
    
    // create return statement for lambda 
    Expression setupProperty = Expression.Property(value, "Bar"); 
    
    // convert expression to lambda (should now be the equivalent of "v => v.Bar") 
    var func = Expression.Lambda<Func<IFoo, string>>(setupProperty, value);
    
    var mockFoo = new Mock<IFoo>(); 
    mockFoo.SetupProperty(func); // this works now
    mockFoo.Object.Bar = "Burge+"; 
    

    I investigated this by creating an expression from a lambda using the code below

    Expression<Func<IFoo, string>> setupBar = v => c.Bar;
    

    I then looked at this in the debugger in vs 2010. Expressions have a “Debug View” that shows a text representation of the expression so it is possible to add a watch on that or something similar. The above comes out as

     .Lambda #Lambda1<System.Func`2[WindowsFormsApplication1.IFoo,System.String]>(WindowsFormsApplication1.IFoo
     $v) {
       $v.Bar
     }
    

    I looked at this and tried to work out what Expressions would make this, then created an expression and compared it in the debugger.

    The interesting thing for me is that although this expression returns a value there is no assignment or return statement. I guess this must be implicit somehow.

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

Sidebar

Related Questions

I'm trying to write a piece of code that will do the following: Take
Is it possible to write code in a Flex application that will only be
Even though it is possible to write generic code in C using void pointer(generic
I would like to write the following code: boolean found = false; search(new SearchCallback()
Is it possible to write code template or a snippet which will do following:
Is it possible to vectorise code like the following? length(x) <- 100; x[1] <-
What would like to able to write in my code is the following. c²
Is it possible to write union select queries like the following more succintly? select
Is it possible to write an interactive defun with code r that has an
Is it possible to write a doctest unit test that will check that an

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.