我可以使用Expression< Func< T,bool>并可靠地查看在Func< T,bool>中引用了哪些属性? [英] Can I use Expression<Func<T, bool>> and reliably see which properties are referenced in the Func<T, bool>?
问题描述
我正在编写具有Enumerable.Where
风格的东西,该谓词的形式为Func<T, bool>
.如果基础T
实现了INotifyPropertyChanged
,那么我想对重新评估谓词更加聪明.
I'm writing something in the flavour of Enumerable.Where
in that takes a predicate of the form Func<T, bool>
. If the underlying T
implements INotifyPropertyChanged
, I'd like to be a bit more intelligent about re-evaluating the predicate.
我正在考虑将其更改为使用Expression<Func<T, bool>>
,然后使用表达式树来查找谓词中使用的属性.然后,我可以让我的PropertyChanged
处理程序变得更聪明.
I'm thinking about changing it to use Expression<Func<T, bool>>
, and then using the expression tree to find out which properties are used in the predicate. Then I can have my PropertyChanged
handler be a bit more intelligent.
我的问题:这可行吗?如果谓词很简单(例如x => x.Age > 18
),则Expression
似乎包含了我需要的所有内容.在某些情况下,我将无法看到所引用的属性?
My question: is this feasible? If the predicate's simple (e.g. x => x.Age > 18
), then the Expression
seems to have everything I need in it. Are there scenarios where I won't be able to see which properties are referenced?
推荐答案
是的,您将能够直接看到 所引用的所有内容.当然,如果有人通过
Yes, you'll be able to see everything directly referenced. Of course, if someone passes
x => ComputeAge(x) > 18
那么您不一定会知道ComputeAge
是指Age
属性.
then you won't necessarily know that ComputeAge
refers to the Age
property.
表达式树将准确地表示lambda表达式中的内容.
The expression tree will be an accurate representation of exactly what's in the lambda expression.
这篇关于我可以使用Expression< Func< T,bool>并可靠地查看在Func< T,bool>中引用了哪些属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!