Loading,你用IE,难怪你打不开

Lambda获取类属性的名字


using System;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Reflection;



public
static class ClassExt { private static MemberInfo GetMemberInfo(Expression expression) { LambdaExpression lambdaExpression = (LambdaExpression)expression; MemberExpression memberExpression = (!(lambdaExpression.Body is UnaryExpression)) ? ((MemberExpression)lambdaExpression.Body) : ((MemberExpression)((UnaryExpression)lambdaExpression.Body).Operand); return memberExpression.Member; } public static string PropName<TProperty>(this object obj, Expression<Func<TProperty>> property) { return GetMemberInfo(property).Name; } } class A { public int Prop { get; set; } private void Example() { string propName = this.PropName(() => Prop); Console.WriteLine(propName); //Prop } }

 

posted @ 2020-12-04 09:58  老板娘的神秘商店  阅读(291)  评论(0编辑  收藏  举报