static void Main(string[] args) { for (int i = 0; i < 3; i++) { for (int j = 2; j < 4; j++) { Debug.Print("----------"); Debug.Print(DebugMsg(() => i)); Debug.Print(DebugMsg(() => j)); } } } public static string DebugMsg<T>(Expression<Func<T>> expr) { var body = expr.Body as MemberExpression; var name = body.Member.Name; var value = ((FieldInfo)body.Member).GetValue(((ConstantExpression)body.Expression).Value); return name + " = " + value; }
---------- i = 0 j = 2 ---------- i = 0 j = 3 ---------- i = 1 j = 2 ---------- i = 1 j = 3 ---------- i = 2 j = 2 ---------- i = 2 j = 3