1

How can I use Expression to set properties on an instance of an object I already have? I don't want to create a new object, so Expression.New where you can pass in member initializers won't work.

This is not a duplicate of How to set property value using Expressions? because that takes an Expression and sets properties via Reflection.

I have an existing object and want to create an Expression where I can set properties. That expression will then get compiled into a delegate to execute.

2
  • Possible duplicate of How to set property value using Expressions? Commented Sep 22, 2017 at 22:43
  • 2
    Expression.Assign into Expression.Property of your object (either as a ConstantExpression or ParameterExpression depending on how you're using it) passing the PropertyInfo of the property you want to assign Commented Sep 22, 2017 at 23:04

2 Answers 2

3

what about:

public class User {
    public string Name {get;set;}
}

public static void Main()
{
    var user = new User();
    var assigner = GetAssigner<User, string>(u => u.Name);
    assigner.Compile()(user, "Joe");
    Console.WriteLine(user.Name);
}

public static Expression<Action<TClass, TValue>> GetAssigner<TClass, TValue>(Expression<Func<TClass, TValue>> propertyAccessor){
    var prop = ((MemberExpression)propertyAccessor.Body).Member;
    var typeParam = Expression.Parameter(typeof(TClass));
    var valueParam = Expression.Parameter(typeof(TValue));
    return Expression.Lambda<Action<TClass, TValue>>(
        Expression.Assign(
            Expression.MakeMemberAccess(typeParam, prop),
            valueParam), typeParam, valueParam);

}

Remember that the expression is just a definition of what should be done, you have to compile it to a delegate to be able to invoke it.

Sign up to request clarification or add additional context in comments.

1 Comment

The combination of Expression.Assign with Expression.MakeMemberAccess is exactly what I was looking for.
1

Have you looked at using MemberAssignment to set the value of the member(s)?

Represents assignment operation for a field or property of an object.

Important note in the remarks, however: you can't create these directly. Instead,

Use the Bind factory methods to create a MemberAssignment. A MemberAssignment has the BindingType property equal to Assignment.

4 Comments

I have a list of MemberAssignments. How do I create an Expression to assign those to an existing object? I can do it when creating a new object via Expression.New, but I don't know how to do it with an existing object.
It’s been a while, but I seem to recall MemberAssignment being specifically for object initializer expressions, like new X { Member = y }. A plain old Assign(Property(x, "Member"), y), should be fine.
The docs don't mention object initializer expressions, perhaps you're thinking of MemberInitExpression?
@JoshClose pass the property to get/set as a ParameterExpression after all, property setter are really just method calls accepting a parameter corresponding to the value to update

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.