我有一个示例数据类
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属性.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。