C#中根据变量获取变量名字符串
- /// <summary>
- /// 获取当前变量的变量名 字符串
- /// 调用:GetVarName(p=>test.str1); 返回 "str1"
- /// </summary>
- /// <param name="exp"></param>
- /// <returns></returns>
- public static string GetVarName(System.Linq.Expressions.Expression<Func<string, string>> exp)
- {
- return ((System.Linq.Expressions.MemberExpression)exp.Body).Member.Name;
- }
随性随心,随风随行!