views:

57

answers:

3

This is a very complicated question even though it looks simple. I think I would have to traverse the entire expression tree unless someone knows a better way.

Let's say I have a user object

class User
{
    public UserAccount Account {get;set;}
}
class UserAccount
{
    public Name {get;set;}
}
var user = new User() { Account = new UserAccount()};

How could I use linq expressions to set the property of Name

SetValue(c => c.Account.Name, "Test");
+1  A: 

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 => c.Account.Name.Length, 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) => c.Account.Name = 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.

Jon Skeet
That's an interesting change but it would make my code a lot less explicit.
Kyle
I wonder if I could modify the tree to be that...
Kyle
@Kyle: You can't modify expression trees, but you could potentially build a new expression tree.
Jon Skeet
This just threw away about 5 hours and 20 lines of really annoying traversing code. Thank you
Kyle
A: 

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 => c.Account.Name = "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.

Darin Dimitrov
+1  A: 

http://geekswithblogs.net/EltonStoneman/archive/2009/11/05/retrieving-nested-properties-from-lambda-expressions.aspx

Kyle
this link provided me with enough understanding to be able to achieve what the question was asking.
kdawg