Linq的delegate表達式,Insus.NET覺得它封裝得好,讓開發時簡化了很多代碼,而且容易閱讀與檢索。 比如,我們需要計算優惠給客戶金額,打85%折,可以這樣寫: using System; using System.Collections.Generic; using System.Li ...
Linq的delegate表達式,Insus.NET覺得它封裝得好,讓開發時簡化了很多代碼,而且容易閱讀與檢索。
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Linq.Expressions; namespace Insus.NET.Utilities { public class Class2 { public decimal Preferential(decimal amount) { return amount * (1 - 0.85m); //amount - amount * 0.85m; } } }Source Code
接下來,Insus.NET還想使用一個表達式樹的類型Expression<TDelegate>實現:
![](http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Linq.Expressions; namespace Insus.NET.Utilities { public delegate decimal pref(decimal amount); public class Class2 { //public decimal Preferential(decimal amount) //{ // return amount * (1 - 0.85m); //amount - amount * 0.85m; //} // public pref Preferential11 = a => (a * (1 - 0.85m)); static Expression<pref> expr = amount => (amount - amount * 0.85m); public pref Preferential2 = expr.Compile(); } }Source Code