Accepted answer

well, you'd not only have to traverse the expression tree: you'd also have to convert final property "getter" you encounter into a property "setter". essentially you'd want to find the expression which acts as the "target" of the getter (i.e. the object it's going to get the property of), evaluate it to get the target, then find the corresponding setter for the final property, and call it with the target and the new value.

note that by only requiring the expression tree to represent the "getter", you're losing some of the compile-time safety you might expect... because a caller could pass in a read-only property:

setvalue(c =>, 0); // string.length is read-only

another alternative would be to change your code to make the lambda expression represent the setter instead:

setvalue((c, value) => = value, "test");

then you wouldn't even need an expression tree - you could use a simple delegate, and just execute it appropriately.

unfortunately you haven't really given us enough information about what you're trying to achieve to know whether this is a feasible suggestion.


yes, you will have to traverse the whole expression tree which if you want to work in the general case might be challenging. can't you just do this instead:

setvalue<user>(c => = "test");

where setvalue is defined like this:

public void setvalue<t>(action<t> action)

or without the generic parameter if this will work only with user.

Related Query

More Query from same tag