C#中根据变量获取变量名字符串

  1. /// <summary>
  2.         /// 获取当前变量的变量名 字符串
  3.         /// 调用:GetVarName(p=>test.str1); 返回 "str1"
  4.         /// </summary>
  5.         /// <param name="exp"></param>
  6.         /// <returns></returns>
  7.         public static string GetVarName(System.Linq.Expressions.Expression<Func<string, string>> exp)
  8.         {
  9.            return ((System.Linq.Expressions.MemberExpression)exp.Body).Member.Name;
  10.         }
posted @ 2014-04-04 17:25  皓月空  阅读(2397)  评论(0编辑  收藏  举报