Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static void Swap<T>(Expression<Func<T>> left, Expression<Func<T>> right)
{
var lvalue = left.Compile()();
Expression.Lambda<Func<T>>(Expression.Assign(left.Body, Expression.Constant(right.Compile()()))).Compile()();
Expression.Lambda<Func<T>>(Expression.Assign(right.Body, Expression.Constant(lvalue))).Compile()();
}
public class Test
{
public int X { get; set; }
public int Y { get; set; }
}
static void Main()
{
var obj = new Test() { X = 123, Y = 456, };
Swap(() => obj.X, () => obj.Y);
Console.WriteLine("obj.X = {0}, obj.Y = {1}", obj.X, obj.Y);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment