Can I get specific metadata from a Func?

前端 未结 4 573
逝去的感伤
逝去的感伤 2021-02-04 17:16

Consider the following code:

string propertyName;
var dateList = new List() { DateTime.Now };
propertyName = dateList.GetPropertyName(dateTimeObj         


        
4条回答
  •  -上瘾入骨i
    2021-02-04 17:52

    Something like this should do the trick:

    public static string GetPropertyName(this IList list, Expression> expr) {
      MemberExpression member_expression = expr.Body as MemberExpression;
      if (member_expression == null)
          throw new ArgumentNullException("member_expression");
      MemberInfo member = member_expression.Member;
      PropertyInfo property = member as PropertyInfo;
      string proname = memeber.name;
    }
    

    WARNING: Air Code!

提交回复
热议问题