.Net 扩展的使用
Product.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LanguageFeatures.Models
{
//public class Product
//{
// private int productID;
// private string name;
// private string description;
// private decimal price;
// private string category;
// public int ProductID
// {
// get { return productID; }
// set { productID = value; }
// }
// public string Name
// {
// get { return name; }
// set { name = value; }
// }
// public string Description
// {
// get { return description; }
// set { description = value; }
// }
// public decimal Price
// {
// get { return price; }
// set { price = value; }
// }
// public string Category
// {
// get { return category; }
// set { category = value; }
// }
//}
// 自动属性
//public class Product
//{
// public int ProductID { get; set; }
// public string Name { get; set; }
// public string Description { get; set; }
// public decimal Price { get; set; }
// public string Category { set; get; }
//}
// 结合起来
public class Product
{
private string name;
public int ProductID { get; set; }
public string Name
{
get
{
return ProductID + name;
}
set
{
name = value;
}
}
public string Description { get; set; }
public decimal Price { get; set; }
public string Category { set; get; }
}
}
ShoppingCart.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LanguageFeatures.Models
{
public class ShoppingCart
{
public List<Product> Products { get; set; }
}
}
MyExtensionMethods.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LanguageFeatures.Models
{
public static class MyExtensionMethods
{
public static decimal TotalPrices(this ShoppingCart cartParam)
{
decimal total = 0;
foreach (Product prod in cartParam.Products)
{
total += prod.Price;
}
return total;
}
}
}
Controller
public ViewResult UseExtension()
{
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)); //Total: ¥378.40
}
IEnumerable 改造
ShoppingCart.cs
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LanguageFeatures.Models
{
public class ShoppingCart:IEnumerable<Product>
{
public List<Product> Products { get; set; }
public IEnumerator<Product> GetEnumerator()
{
return Products.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
MyExtensionMethods.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace LanguageFeatures.Models
{
public static class MyExtensionMethods
{
public static decimal TotalPrices(this IEnumerable<Product> ProductEnum)
{
decimal total = 0;
foreach (Product prod in ProductEnum)
{
total += prod.Price;
}
return total;
}
}
}
Controller
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}
}
};
// create and populate an array of Product objects
Product[] productArray = {
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}
};
// get the total value of the products in the cart
decimal cartTotal = products.TotalPrices();
decimal arrayTotal = productArray.TotalPrices();
return View("Result",
(object)String.Format("Cart Total: {0}, Array Total: {1}",
cartTotal, arrayTotal));
}
.Net 扩展的使用的更多相关文章
- Asp.net Boilerplate之AbpSession扩展
当前Abp版本1.2,项目类型为MVC5. 以属性的形式扩展AbpSession,并在"记住我"后,下次自动登录也能获取到扩展属性的值,版权归"角落的白板报"所 ...
- 恢复SQL Server被误删除的数据(再扩展)
恢复SQL Server被误删除的数据(再扩展) 大家对本人之前的文章<恢复SQL Server被误删除的数据> 反应非常热烈,但是文章里的存储过程不能实现对备份出来的日志备份里所删数据的 ...
- .NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法
.NET Core中间件的注册和管道的构建(3) ---- 使用Map/MapWhen扩展方法 0x00 为什么需要Map(MapWhen)扩展 如果业务逻辑比较简单的话,一条主管道就够了,确实用不到 ...
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...
- 采用EntityFramework.Extended 对EF进行扩展(Entity Framework 延伸系列2)
前言 Entity Framework 延伸系列目录 今天我们来讲讲EntityFramework.Extended 首先科普一下这个EntityFramework.Extended是什么,如下: 这 ...
- Dapper扩展之~~~Dapper.Contrib
平台之大势何人能挡? 带着你的Net飞奔吧!http://www.cnblogs.com/dunitian/p/4822808.html#skill 上一篇文章:Dapper逆天入门~强类型,动态类型 ...
- ExtJS 4.2 Date组件扩展:添加清除按钮
ExtJS中除了提供丰富的组件外,我们还可以扩展他的组件. 在这里,我们将在Date日期组件上添加一个[清除]按钮,用于此组件已选中值的清除. 目录 1. Date组件介绍 2. 主要代码说明 3. ...
- .NET Core的文件系统[5]:扩展文件系统构建一个简易版“云盘”
FileProvider构建了一个抽象文件系统,作为它的两个具体实现,PhysicalFileProvider和EmbeddedFileProvider则分别为我们构建了一个物理文件系统和程序集内嵌文 ...
- Hawk 6. 编译和扩展开发
Hawk是开源项目,因此任何人都可以为其贡献代码.作者也非常欢迎使用者能够扩展出更有用的插件. 编译 编译需要Visual Stuido,版本建议使用2015, 2010及以上没有经过测试,但应该可以 ...
- 为IEnumerable<T>添加RemoveAll<IEnumerable<T>>扩展方法--高性能篇
最近写代码,遇到一个问题,微软基于List<T>自带的方法是public bool Remove(T item);,可是有时候我们可能会用到诸如RemoveAll<IEnumerab ...
随机推荐
- [NOIP2015模拟10.27] [JZOJ4270] 魔道研究 解题报告(动态开点+权值线段树上二分)
Description “我希望能使用更多的魔法.不对,是预定能使用啦.最终我要被大家称呼为大魔法使.为此我决定不惜一切努力.”——<The Grimoire of Marisa>雾雨魔理 ...
- sicily 1003. hash
Description 请用HASH链式法来解决冲突,且规定链表在链表头插入新元素. 规定HASH函数为:h(x) = x % 11,即哈希数组下标为0-10. 给定两种操作: I 操作,插入一个新的 ...
- [学习笔记]node.js中的path.extname方法
path.extname 返回path路径文件扩展名,如果path以 ‘.' 为结尾,将返回 ‘.',如果无扩展名 又 不以'.'结尾,将返回空值. path.extname('index.html' ...
- VS 代码打包工具
源代码下载地址 https://github.com/loresoft/msbuildtasks
- Perl模块利用CPAN在线安装自动化
需要解决2个问题: 1. 如何与CPAN交互:利用perl –MCPAN –e ‘install 模块’ 2. 如何安装指定的版本:作者/模块-版本.tar.gz How to install a ...
- C语言操作数截断
//测试截断 #include <stdio.h> int main() { int a = 0x80000001; unsigned int b = 0x80000001; printf ...
- 【Henu ACM Round#20 C】 Eevee
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理处所有的字符串可能的样子. 存在map里面就好. [代码] #include <bits/stdc++.h> usi ...
- 【Codeforces Round #421 (Div. 2) A】Mister B and Book Reading
[题目链接]:http://codeforces.com/contest/820/problem/A [题意] 每天看书能看v页; 且这个v每天能增加a; 但是v有上限v1; 然后每天还必须往回看t页 ...
- ECNUOJ 2615 会议安排
会议安排 Time Limit:1000MS Memory Limit:65536KB Total Submit:451 Accepted:102 Description 科研人员与相关领域的国内外同 ...
- COGS——T 21. [HAOI2005] 希望小学
http://www.cogs.pro/cogs/problem/problem.php?pid=21 ★★ 输入文件:hopeschool.in 输出文件:hopeschool.out ...