MVC 中使用扩展方法
扩展方法(Extension Method)是给那些不是你拥有、因而不能直接修改的类添加方法的一种方便的办法。
一、使用扩展方法
1、定义一个购物车的类-ShoppingCart
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Demo.Models
{
public class ShoppingCart:IEnumerable<Product>
{
public List<Product> Products { get; set; } }
}
2、定义一个扩展方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Demo.Models
{
public static class MyExtensionMethods
{
public static decimal TotalPrices(this ShoppingCart cartParam)
{
decimal total = ;
foreach (Product prod in cartParam.Products)
{
total += prod.Price;
}
return total;
} }
}
this 关键字把TotalPrices定义为一个扩展方法 ShoppingCart 告诉。net 这个扩展方法运用与那个类
3、运用扩展方法
public ViewResult UserExtension()
{
//创建并填充ShoppingCart
ShoppingCart cart = new ShoppingCart
{
Products = new List<Product>{
new Product{Name="kayak",Price=275M},//皮划艇
new Product{Name="Lifejacket",Price=48.95M},//休闲夹克
new Product{Name="Soccer ball",Price=19.50M},//足球
new Product{Name="Corner flag",Price=34.95M}//角旗
}
};
//求去购物车中的产品总价
decimal cartTotal = cart.TotalPrices();
return View("Result", (object)String.Format("Total:{0:c}", cartTotal));
}
4、结果展示

二、对接口运用扩展方法
1、在ShoppingCart类中实现接口
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Demo.Models
{
public class ShoppingCart:IEnumerable<Product>
{
public List<Product> Products { get; set; }
public IEnumerator<Product> GetEnumerator()
{
return Products.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
2、在接口上工作的扩展方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Demo.Models
{
public static class MyExtensionMethods
{ public static decimal TotalPrices(this IEnumerable<Product> productEnum)
{
decimal total=;
13 foreach(Product prod in productEnum)
14 {
15 total += prod.Price;
}
return total;
}
}
}
3、将扩展方法运用于同一接口的不同实现
public ViewResult UseExtensionEnumerable()
{
IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product>{
new Product{Name="kayak",Price=275M},//皮划艇
new Product{Name="Lifejacket",Price=48.95M},//休闲夹克
new Product{Name="Soccer ball",Price=19.50M},//足球
new Product{Name="Corner flag",Price=34.95M}//角旗
}
};
Product[] productArary ={
new Product{Name="kayak",Price=375M},//皮划艇
new Product{Name="Lifejacket",Price=48.95M},//休闲夹克
new Product{Name="Soccer ball",Price=19.50M},//足球
new Product{Name="Corner flag",Price=34.95M}//角旗
};
//获取购物车中的产品总价
decimal cartTotal = products.TotalPrices();
//获取数组中产品的总价
decimal arrayTotal = productArary.TotalPrices();
return View("Result",(object)String.Format("Cart Total:{0},Array Total:{1}",cartTotal,arrayTotal));
}
4、结果展示

三、创建过滤扩展方法
1、
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web; namespace Demo.Models
{
public static class MyExtensionMethods
{
public static IEnumerable<Product> FilterByCategory(this IEnumerable<Product> productEnum, string categoryParm)
{
foreach (Product prod in productEnum)
{
if (prod.Category == categoryParm)
{
yield return prod;
}
}
}
}
}
2、使用过滤扩展方法
public ViewResult UseFilterExtensionMethod()
{
IEnumerable<Product> products = new ShoppingCart
{
Products = new List<Product>{
new Product{Name="kayak",Category="Watersports",Price=375M},//皮划艇
new Product{Name="Lifejacket",Category="Watersports",Price=48.95M},//休闲夹克
new Product{Name="Soccer ball",Category="Soccer",Price=19.50M},//足球
new Product{Name="Corner flag",Category="Soccer",Price=34.95M}//角旗
}
};
decimal total = ;
foreach (Product prod in products.FilterByCategory("Soccer"))
{
total += prod.Price;
}
return View("Result",(object)String.Format("Total:{0}",total));
}
3、结果展示

只用Soccer分类中的价格被返回累加出来。
MVC 中使用扩展方法的更多相关文章
- ASP.NET + MVC5 入门完整教程四---MVC 中使用扩展方法
https://blog.csdn.net/qq_21419015/article/details/80433640 1.示例项目准备1)项目创建新建一个项目,命名为LanguageFeatures ...
- Enum扩展及MVC中DropDownListFor扩展方法的使用
public enum SearchState { /// <summary> /// 全部 /// </summary> [Description("全部" ...
- mvc给html扩展方法:
mvc给html扩展方法: 注意:扩展方法和所在的类都必须是 public static如果在页面直接使用新扩展的方法,需要web.config里把Web.Helper名称命名空间加上,页面才能访问到 ...
- Mvc 分页栏扩展方法
using System; using System.Collections.Generic; using System.Reflection; using System.Text; using Sy ...
- C#3.0中的扩展方法
在实际应用中,开发者完成代码的编译后,除非重新编译更改后的代码,否则开发者很难在原有代码中添加新的功能. 在C#3.0中,提供了一个扩展方法的新特性,可以使得开发者在编译后的程序集里边添加相关的方法, ...
- 记录C#中的扩展方法
C#中的扩展方法. 系统自带的类型,我们无法去修改: 修改源代码需要较大的精力,而且可能会带来错误: 我们只是需要一个或者较少的几个方法,修改源代码费时费力: 被扩展的类是sealed的,不能被继承: ...
- C#编程(六十一)------------LINQ中的扩展方法
原文链接: http://blog.csdn.net/shanyongxu/article/details/47208401 LINQ中的扩展方法 LINQ中where扩展方法,要想使用,必须导入us ...
- objective-C中的扩展方法与partial class
在c#中要扩展一个现有类非常easy,比方这样: ? 1 2 3 4 5 6 7 public static class Utils { public static void PrintTo ...
- C#中的扩展方法(向已有类添加方法,但无需创建新的派生类型)
C#中的扩展方法 扩展方法使你能够向现有类型"添加"方法,而无需创建新的派生类型.重新编译或以其他方式修改原始类型. 扩展方法是一种特殊的静态方法,但可以像扩展类型上的实例方法一样 ...
随机推荐
- Codeforces Round #242 (Div. 2) C题
题目链接:http://codeforces.com/contest/424/problem/C, 想来一个小时,就是做不出,都做出来了,悲剧! 分析:我们知道交换异或的顺序不影响答案! 然后就是求t ...
- CSS中的长度值
以下总结来自慕课网(依然比较浅显). 长度单位总结一下,目前比较常用到px(像素).em.% 百分比,要注意其实这三种单位都是相对单位. 1.像素 像素为什么是相对单位呢?因为像素指的是显示器上的小点 ...
- spring_150804_controller
实体类: package com.spring.model; public class DogPet { private int id; private String name; private in ...
- java中静态代理,动态代理知识的补充
文章转载自:http://blog.csdn.net/jialinqiang/article/details/8950989 一.Java动态代理 相对于静态代理的代理类在编译时生成(.class文件 ...
- 物联网操作系统Hello China移植mile stone之一:移植基础版本V1.76发布
Hello China V1.76版发布,这是向ARM系列CPU移植的基础版本.相对V1.75版,该版本主要做了如下的一些调整: 1. 通过宏定义的方式对内核实现了模块化,开发者可以通过开启或关闭预 ...
- Struts2笔记——利用token防止表单重复提交
在一些项目中经常会让用户提交表单,当用户点击按钮提交后,如果再次浏览器刷新,这就会造成表单重复提交,若是提交的内容上传至服务器并请求数据库保存,重复提交的表单可能会导致错误,然后跳转到错误界面,这是一 ...
- Java:Object类
objcet类中涉及的多态的扩展性,由于Object是所有类的根类,所以它可以接收任意类型的数据,包括基本数据类型.因为这一特点,它可以对多态性进行扩展. 1.创建一个Demo类来判断类类型 clas ...
- Objective-C:自定义Block函数
Block函数是一种类似于函数指针的函数,程序员只需要把需要操作的代码封装到定义的block中即可,以后需要使用时,直接调用,非常方便.... 举例如下: 第一种形式:自定义一个无返回值而且无参数的b ...
- logstash_agent.conf 语法注意事项
编写配置文件时要注意语法,如新版本的logstash对参数host变更为hosts,去除了port参数等. [root@localhost logstash]# cat logstash_agent. ...
- 开发ProxyServer的时候如何在一台PC上调试
为了测试在真实的网络环境下你的ProxyServer性能如何,而你手头又只有一台电脑,怎么办? 打开你的ProxyServer(我用java写的,因此ProxyServer的进程是javaw.exe) ...