c# 获取变量名称

示例:

public class Test
{
	public int tInt;
	public bool tBool;
}
var t = new Test();
Console.WriteLine(t.tInt) ///打印 tInt
public static string GetFieldName(object obj)
{
    Expression<Func<object>> expr = () => obj;
    if (expr.Body is MemberExpression memberExpr)
    {
       return memberExpr.Member.Name;
    }
    throw new ArgumentException("非法", nameof(obj));
}

posted on 2024-07-16 10:52  yungs  阅读(77)  评论(0编辑  收藏  举报