.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));
        }

本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6895470.html,如需转载请自行联系原作者
相关文章
|
1月前
|
PHP Windows
php扩展com_dndnet(PHP与.NET框架进行交互)
php扩展com_dndnet(PHP与.NET框架进行交互)
php扩展com_dndnet(PHP与.NET框架进行交互)
|
开发框架 程序员 API
【C#】.net core2.1,通过扩展状态代码页方法对404页面进行全局捕抓并响应信息
在开发一个网站项目时,除了异常过滤功能模块,还需要有针对404不存在的api接口和页面处理功能 本篇文章就来讲讲,如何自定义全局请求状态类来统一处理
191 0
net netcore2 netcore3 HtmlHelper扩展(checkboxlistfor)为例
net HtmlHelper扩展(checkboxlistfor)为例 netcore2 IHtmlHelper扩展(checkboxlistfor)为例 netcore3 IHtmlHelper扩展(checkboxlistfor)为例
854 0
|
SQL 存储 XML
ASP.NET Core 实战:基于 Dapper 扩展你的数据访问方法
ASP.NET Core 实战:基于 Dapper 扩展你的数据访问方法 一、前言 在非静态页面的项目开发中,必定会涉及到对于数据库的访问,最开始呢,我们使用 Ado.Net,通过编写 SQL 帮助类帮我们实现对于数据库的快速访问,后来,ORM(Object Relational Mapping,对象关系映射)出现了,我们开始使用 EF、Dapper、NHibernate,亦或是国人的 SqlSugar 代替我们原来的 SqlHelper.cs。
4157 0
|
SQL 前端开发 .NET
Asp.net 面向接口可扩展框架之数据处理模块及EntityFramework扩展和Dapper扩展(含干货)
原文:Asp.net 面向接口可扩展框架之数据处理模块及EntityFramework扩展和Dapper扩展(含干货) 接口数据处理模块是什么意思呢?实际上很简单,就是使用面向接口的思想和方式来做数据处理。
1146 0
|
缓存 C#
使用.NET Core搭建分布式音频效果处理服务(四)选择垂直扩展还是水平扩展?
众所周知垂直扩展是提升单机的性能的方式,比如提升双路、四路的CPU运算能力,加大内存,更换速度更快的SSD,或者从代码根本上进行优化和性能提升。水平扩展是提供多台多种服务器分离单机性能的方式,比如集群,主从,队列,负载平衡等等。
1069 0