Created
May 2, 2010 15:55
-
-
Save takeshik/387230 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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