We have covered the topic of expression tree and lambda - serie 1. Serie 2 is like a complementary to serie 1 to cover something that is not the core but may come useful when you are in need of it.
Refactor-Proof references to members
If you are doing some WPF programming you use extensivly the interface called INotifyProeprtyChanged, you may be aware of the refactoirng problem where the properyt name is encoded in the string, which makes the refactoring tools fails to work.
The same has been true that if you are doing reflectoin based programing, where if you wan to achieve the function of "use the property of BirthDate defined in my type" you have to use string.
With the Expression Tree, you can build a Expression tree representing a property reference using a lambda expression.
The the method can dissect the expresion tree and work ou thte property you mean.
As an example of how it can be used, let's see the following
serializationContext.AddProperty(x => x.BirthDate);
Simpler Reflection
There are some restriction in Generic in C# that generics does not play nicesly with arithmetic operators.
The key is to provide a generic Operator class and a nongeneric helper as follow.
T runningTotal = initialValue;
foreach (T item i n value)
{
runningTotal = Operator.Add(runningTotal, values);
}
TODO:
write some code to go through the use cases.
本文探讨了在WPF编程中使用INotifyPropertyChanged接口时遇到的反射问题,特别是当属性名称编码在字符串中导致重构工具失效的情况。通过引入表达式树,可以构建代表属性引用的Lambda表达式,并通过解析表达式树来解决问题。此外,文章还讨论了泛型限制下C#中算术运算符的问题,并提供了解决方案。
1063

被折叠的 条评论
为什么被折叠?



