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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T23:01:22+00:00 2026-05-14T23:01:22+00:00

Given the following code… class Program { static void Main(string[] args) { Foo foo

  • 0

Given the following code…

class Program {

    static void Main(string[] args) {

        Foo foo = new Foo { Bar = new Bar { Description= "Martin" }, Name = "Martin" };

        DoLambdaStuff(foo, f => f.Name);
        DoLambdaStuff(foo, f => f.Bar.Description);

    }

    static void DoLambdaStuff<TObject, TValue>(TObject obj, Expression<Func<TObject, TValue>> expression) {

        // Set up and test "getter"...

        Func<TObject, TValue> getValue = expression.Compile();

        TValue stuff = getValue(obj);

        // Set up and test "setter"...

        ParameterExpression objectParameterExpression = Expression.Parameter(typeof(TObject)), valueParameterExpression = Expression.Parameter(typeof(TValue));
        Expression<Action<TObject, TValue>> setValueExpression = Expression.Lambda<Action<TObject, TValue>>(
            Expression.Block(
                Expression.Assign(Expression.Property(objectParameterExpression, ((MemberExpression)expression.Body).Member.Name), valueParameterExpression)
            ), objectParameterExpression, valueParameterExpression
        );
        Action<TObject, TValue> setValue = setValueExpression.Compile();


        setValue(obj, stuff);

    }

}

class Foo {

    public Bar Bar { get; set; }
    public string Name { get; set; }

}

class Bar {

    public string Description{ get; set; }

}

The call to DoLambdaStuff(foo, f => f.Name) works ok because I am accessing a shallow property, however the call to DoLambdaStuff(foo, f => f.Bar.Description) fails – although the creation of the getValue function works fine, the creation of the setValueExpression fails because I am attempting to access a deep property of the object.

Can anybody please help me to modify this so that I can create the setValueExpression for deep properties as well as shallow?

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-14T23:01:23+00:00Added an answer on May 14, 2026 at 11:01 pm

    You need to take advantage of the fact that your expression.Body is already representing the property you want to set. This means you can use expression.Body as the left-hand-side in your assignment expression:

        public static void Main(string[] args)
        {
            Foo foo = new Foo { Bar = new Bar { Name = "Martin", Buzz = new Fiz() { Name = "Carl" }}, Name = "Martin" };
    
            DoLambdaStuff(foo, f => f.Bar.Name, "Dennis");
            DoLambdaStuff(foo, f => f.Bar.Buzz.Name, "Dennis");
            Console.WriteLine(foo.Bar.Name);
            Console.WriteLine(foo.Bar.Buzz.Name);
    
        }
        static void DoLambdaStuff<TObject, TValue>(TObject obj, Expression<Func<TObject, TValue>> expression, TValue valueToSet)
        {
            // Getter.
            Func<TObject, TValue> getter = expression.Compile();
            TValue stuff = getter(obj);
    
            ParameterExpression pObj = expression.Parameters[0];
            ParameterExpression pValue = Expression.Parameter(typeof (TValue), "value");
            var setterBlock = Expression.Block(
                Expression.Assign(expression.Body, pValue)
                );
    
            var setterExpression = Expression.Lambda<Action<TObject, TValue>>(setterBlock, pObj, pValue);
            Action<TObject, TValue> setter = setterExpression.Compile();
    
            // Test 
            setter(obj,valueToSet);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 539k
  • Answers 539k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer There should be only one center on map. But, you… May 17, 2026 at 2:22 am
  • Editorial Team
    Editorial Team added an answer maybe something like this if you're not using Linq to… May 17, 2026 at 2:22 am
  • Editorial Team
    Editorial Team added an answer You're not alone! We have used both, mistakenly starting with… May 17, 2026 at 2:22 am

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Given the following code: using System.Collections.Generic; static class Program { static void Main() {
Given the following code: void foo( int* array ) { // ... } void
Given the following code: static member private getIntValue (_map:Map<string, int>) (_key:string) = if (_map.ContainsKey
Given the following code: var people = new List<person>(){ new person { Name =
Given the following code: Class User{ Task m_Task; public function getTask(Do work) { return
Given the following code: a = 0 def foo(): # global a a +=
given the following code: public class MainFrame extends JFrame{ public MainFrame() throws HeadlessException {
Given the following code: class A extends Actor { def act() { loop {
Given the following code: Rect pos = new Rect(); for (int i = 0;
Given the following code (that doesn't work): while True: # Snip: print out current

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.