I'm trying to build a method that can capture assignments from a strongly-typed statement body. The method call should look something like this:
myObject.Assign(o =>
{
o.SomeProperty = 1;
o.AnotherProperty = "two";
});
I'd like to get a list of assignments; an array of MemberAssignment
instances seems fitting.
It's unclear to me what the method header of Assign()
needs to look like.
Here's what I currently have:
public class MyClass
{
public void Assign(System.Linq.Expressions.Expression<System.Action<MyBunchOfProps>> assignments)
{
}
}
public struct MyBunchOfProps
{
public int SomeProperty { get; set; }
public string AnotherProperty { get; set; }
}
With a statement body and multiple assignments, I get "A lambda expression with a statement body cannot be converted to an expression tree".
If I omit the statement body and only do one assignment (myObject.Assign(o => o.SomeProperty = 1);
), I instead get "An expression tree may not contain an assignment operator".
Do I need to use a different class rather than Expression
?