c# - expression tree and lambda - serie 2

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. 

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值