.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));
        }
posted @ 2017-05-23 18:39  TBHacker  阅读(211)  评论(0编辑  收藏  举报