.Net 扩展的使用
2023-09-27 14:24:30 时间
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));
}
相关文章
- 【转载】ASP.NET MVC的过滤器
- Asp.Net_Mvc3.5语法_<%%>的用法
- ASP.NET MVC中jQuery与angularjs混合应用传参并绑定数据
- ASP.NET MVC图片管理(更新)
- java.net.ServerSocket 解析
- 8、ASP.NET MVC入门到精通——View(视图)
- .NET 4.0中的缓存功能
- 分享.NET系统开发过程中积累的扩展方法
- 抛弃强大的TFS ,借助于BugTracker.NET + Visual Source Safe + SourceLink搭建项目开发环境
- 使用ASP.Net WebAPI构建REST服务(五)——客户端
- 如何使用asp.net 1.1中的webcontrol中的TreeView
- Yale CAS + .net Client 实现 SSO(5)--扩展基于数据库的身份验证
- Microsoft .NET Desktop Runtime (Framework)
- .NET(C#) 扩展方法(Extension)
- ASP.NET Core(C#) 字符串常用处理转换的扩展方法工具类
- ASP.NET MVC的Razor引擎:View编译原理
- [ASP.NET MVC]为HtmlHelper添加一个RadioButtonList扩展方法
- [ASP.NET MVC]通过对HtmlHelper扩展简化“列表控件”的绑定
- Asp.net课堂笔记
- .Net时间计算函数,统计某一天是一年的第几周,这一周从哪天开始到哪天结束