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));
}