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 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

Related Questions

Given the following code: using System.Collections.Generic; static class Program { static void Main() {
Given the following code: class C { static void m(String s) { ... }
Given the following code : import java.io.*; public class Main { public static void
Given the following code: class foo { }; class bar: public foo { public:
Given the following code: interface IParam {} class Parameter implements IParam {} void foo(Collection<?
Given the following code: public static class Super { public static class Inner {
Given the following code : public class Game { private Map<String,Coordinate> m_myHashMap; ... //
Given the following code : public abstract class Participant { private String fullName; public
Given the following code: String tmp = new String(\\u0068\\u0065\\u006c\\u006c\\u006f\\u000a); String result = convertToEffectiveString(tmp); //
Given the following code: public interface Selectable { public void select(); } public class

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.