MVC 中使用扩展方法

 扩展方法(Extension Method)是给那些不是你拥有、因而不能直接修改的类添加方法的一种方便的办法。

一、使用扩展方法

1、定义一个购物车的类-ShoppingCart

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 
 7 namespace Demo.Models
 8 {
 9     public class ShoppingCart:IEnumerable<Product>
10     {
11         public List<Product> Products { get; set; }16     }
17 }

2、定义一个扩展方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace Demo.Models
 7 {
 8     public static class MyExtensionMethods
 9     {
10         public static decimal TotalPrices(this ShoppingCart cartParam)
11         {
12             decimal total = 0;
13             foreach (Product prod in cartParam.Products)
14             {
15                 total += prod.Price;
16             }
17             return total;
18         }28     }
29 }

this 关键字把TotalPrices定义为一个扩展方法 ShoppingCart 告诉。net 这个扩展方法运用与那个类

3、运用扩展方法

 

 1 public ViewResult UserExtension()
 2         {
 3             //创建并填充ShoppingCart
 4             ShoppingCart cart = new ShoppingCart
 5             {
 6                 Products = new List<Product>{
 7                 new Product{Name="kayak",Price=275M},//皮划艇
 8                 new Product{Name="Lifejacket",Price=48.95M},//休闲夹克
 9                 new Product{Name="Soccer ball",Price=19.50M},//足球
10                 new Product{Name="Corner flag",Price=34.95M}//角旗
11                 }
12             };
13             //求去购物车中的产品总价
14             decimal cartTotal = cart.TotalPrices();
15             return View("Result", (object)String.Format("Total:{0:c}", cartTotal));
16         }

 

4、结果展示

 二、对接口运用扩展方法

1、在ShoppingCart类中实现接口

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Web;
 6 
 7 namespace Demo.Models
 8 {
 9     public class ShoppingCart:IEnumerable<Product>
10     {
11         public List<Product> Products { get; set; }
12         public IEnumerator<Product> GetEnumerator()
13         {
14             return Products.GetEnumerator();
15         }
16         IEnumerator IEnumerable.GetEnumerator()
17         {
18             return GetEnumerator();
19         }
20     }
21 }

2、在接口上工作的扩展方法

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace Demo.Models
 7 {
 8     public static class MyExtensionMethods
 9     {10         public static decimal TotalPrices(this IEnumerable<Product> productEnum)
11         { 
12             decimal total=0;
13             foreach(Product prod in productEnum)
14             {
15                 total += prod.Price;
16             }
17             return total;
18         }
19     }
20 }

3、将扩展方法运用于同一接口的不同实现

 1  public ViewResult UseExtensionEnumerable()
 2         {
 3             IEnumerable<Product> products = new ShoppingCart
 4             {
 5                 Products = new List<Product>{
 6                     new Product{Name="kayak",Price=275M},//皮划艇
 7                     new Product{Name="Lifejacket",Price=48.95M},//休闲夹克
 8                     new Product{Name="Soccer ball",Price=19.50M},//足球
 9                     new Product{Name="Corner flag",Price=34.95M}//角旗
10                 }
11             };
12             Product[] productArary ={
13                                    new Product{Name="kayak",Price=375M},//皮划艇
14                     new Product{Name="Lifejacket",Price=48.95M},//休闲夹克
15                     new Product{Name="Soccer ball",Price=19.50M},//足球
16                     new Product{Name="Corner flag",Price=34.95M}//角旗
17                                    };
18             //获取购物车中的产品总价
19             decimal cartTotal = products.TotalPrices();
20             //获取数组中产品的总价
21             decimal arrayTotal = productArary.TotalPrices();
22             return View("Result",(object)String.Format("Cart Total:{0},Array Total:{1}",cartTotal,arrayTotal));
23         }

4、结果展示

三、创建过滤扩展方法

1、

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Web;
 5 
 6 namespace Demo.Models
 7 {
 8     public static class MyExtensionMethods
 9     {
10         public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> productEnum, string categoryParm)
11         {
12             foreach (Product prod in productEnum)
13             {
14                 if (prod.Category == categoryParm)
15                 {
16                     yield return prod;
17                 }
18             }
19         }
20     }
21 }

2、使用过滤扩展方法

 1 public ViewResult UseFilterExtensionMethod()
 2         {
 3             IEnumerable<Product> products = new ShoppingCart
 4             {
 5                 Products = new List<Product>{
 6                     new Product{Name="kayak",Category="Watersports",Price=375M},//皮划艇
 7                     new Product{Name="Lifejacket",Category="Watersports",Price=48.95M},//休闲夹克
 8                     new Product{Name="Soccer ball",Category="Soccer",Price=19.50M},//足球
 9                     new Product{Name="Corner flag",Category="Soccer",Price=34.95M}//角旗
10                 }
11             };
12             decimal total = 0;
13             foreach (Product prod in products.FilterByCategory("Soccer"))
14             {
15                 total += prod.Price;
16             }
17             return View("Result",(object)String.Format("Total:{0}",total));
18         }

3、结果展示

只用Soccer分类中的价格被返回累加出来。

posted @ 2015-01-16 11:47  D、boy  阅读(1640)  评论(0编辑  收藏  举报