加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何提取传递给Expression>的属性名称和值?

发布时间:2020-12-15 07:59:05 所属栏目:百科 来源:网络整理
导读:我们假设我有一个这样的方法: public static ListT GetT(this SomeObjectT,ExpressionsFuncT,bool e){//get the property name and value they want to check is true / false...}TheObject().Get(x = x.PropertyName == "SomeValue"); 当我将其传递给Get扩
我们假设我有一个这样的方法:
public static List<T> Get<T>(this SomeObject<T>,Expressions<Func<T,bool>> e){

//get the property name and value they want to check is true / false
...

}

TheObject().Get(x => x.PropertyName == "SomeValue");

当我将其传递给Get扩展方法时,如何获得“PropertyName”和“SomeValue”?

解决方法

我想这就是你所追求的
BinaryExpression expression = ((BinaryExpression)e.Body);
string name = ((MemberExpression)expression.Left).Member.Name;
Expression value = expression.Right;


Console.WriteLine(name);
Console.WriteLine(value);

输出:

PropertyName
SomeValue

错误检查留给读者练习……

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读