I am working on a small amount of code that has the ultimate goal of allowing you to use a property expression to set the value of a property with similar syntax to pass a variable as an out or ref parameter.
Something along the lines of:
public static foo(()=>Object.property, value);
And Object.Property will be assigned a value value.
I use the following code to get an open property object:
public static object GetOwningObject<T>(this Expression<Func<T>> @this) { var memberExpression = @this.Body as MemberExpression; if (memberExpression != null) { var fieldExpression = memberExpression.Expression as MemberExpression; if (fieldExpression != null) { var constExpression = fieldExpression.Expression as ConstantExpression; var field = fieldExpression.Member as FieldInfo; if (constExpression != null) if (field != null) return field.GetValue(constExpression.Value); } } return null; }
Thus, when using the like () => Object.Property property in the expression, return an instance of "Object". I'm a little new to using property expressions, and there seem to be many different ways of doing things, but I want to expand on what I have so far, so given an expression such as () => Foo.Bar.Baz it will give the bar, not Foo. I always need the last contained object in the expression.
Any ideas? Thanks in advance.
source share