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

c# – Lambda Expression`x => x.Property`变为`x =>

发布时间:2020-12-15 08:14:25 所属栏目:百科 来源:网络整理
导读:我有一个示例数据类 public class Data{ public int TestInt { get; set; } public bool TestBool { get; set; } public string TestString { get; set; } public Data() { TestInt = 10; TestBool = true; TestString = "test"; }} 并且是一种扩展方法 publ
我有一个示例数据类
public class Data
{
    public int TestInt { get; set; }
    public bool TestBool { get; set; }
    public string TestString { get; set; }

    public Data() { TestInt = 10; TestBool = true; TestString = "test"; }
}

并且是一种扩展方法

public static void Method<T>(this T item,params Expression<Func<T,object>>[] properties)
{
    /* Some stuff */   
}

我这样用

Data data = new Data();
data.Method(x => x.TestInt,x => x.TestBool,x => x.TestString);

我的方法< T>确实收到3个属性,但它稍微改为:

properties[0] = x => Convert(x.TestId);
properties[1] = x => Convert(x.TestBool);
properties[2] = x => x.TestString;

如您所见,TestString部分未更改.我尝试将我的属性更改为params Expression< Func< T,bool>> []和params Expression< Func< T,int>> []并且只传递相应的参数,它工作正常.我理解问题来自转换为对象,但我无法弄明白.

解决方法

如果要分析原始表达式,一种可能的方法是手动删除转换表达式.

在Method中,您可能会获得UnaryExpression with NodeType = Convert.如果是这样,只需检查此表达式的Operand属性.

(编辑:李大同)

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

    推荐文章
      热点阅读