zoukankan      html  css  js  c++  java
  • delegate-使用笔记

    public class testclass
    {
        public class ProductImages : Page
        {
            protected Repeater rptSmallUrls;
            protected Repeater rptBigUrls;
            /// <summary>
            /// 委托
            /// </summary>
            /// <param name="url"></param>
            delegate void AddUrl(string url);
    
            protected void Page_Load(object sender, EventArgs e)
            {
                int productId = 0;
                int.TryParse(this.Page.Request.QueryString["ProductId"], out productId);
    
                if (!this.Page.IsPostBack)
                {
                    //获取数据
                    ProductInfo productSimpleInfo = ProductBrowser.GetProductSimpleInfo(productId);
                    //绑定相册
                    if (productSimpleInfo != null)
                    {
                        this.BindImages(productSimpleInfo);
                    }
                }
    
            }
    
            private void BindImages(ProductInfo productImageInfo)
            {
                //加载大图
                List<string> bigList = new List<string>();
                AddUrl addUrlToList = delegate (string url)//向List中添加Url的匿名方法
                {
                    if (!string.IsNullOrEmpty(url))
                    {
                        bigList.Add(url);
                    }
                };
                addUrlToList(productImageInfo.ImageUrl1);
                addUrlToList(productImageInfo.ImageUrl2);
                addUrlToList(productImageInfo.ImageUrl3);
                addUrlToList(productImageInfo.ImageUrl4);
                addUrlToList(productImageInfo.ImageUrl5);
                addUrlToList(productImageInfo.ImageUrl6);
                if (productImageInfo.Spread != null && !string.IsNullOrEmpty(productImageInfo.Spread.Images))
                {
                    bigList.AddRange(productImageInfo.Spread.Images.TrimEnd(',').Split(','));
                }
                this.rptBigUrls.DataSource = bigList;
                this.rptBigUrls.DataBind();
    
                //加载小图
                List<string> smallList = new List<string>();
                bigList.ForEach(u => smallList.Add(u.Replace("/product/images/", "/product/thumbs40/40_").Replace("/pet/images/", "/pet/thumbs40/40_")));
                this.rptSmallUrls.DataSource = smallList;
                this.rptSmallUrls.DataBind();
            }
        }
    }
    View Code

    实际使用到的delegate的两种用法。

    记录以供参考。

    一、

    //声明

    delegate void AddUrl(string url);

    //定义

    AddUrl addUrlToList = delegate (string url)
    {
      if (!string.IsNullOrEmpty(url))
      {
        bigList.Add(url);
      }
    };

    //调用

    addUrlToList(productImageInfo.ImageUrl1);

    二、

    //匿名调用

    bigList.ForEach(u => smallList.Add(u.Replace("/product/images/", "/product/thumbs40/40_").Replace("/pet/images/", "/pet/thumbs40/40_")));

  • 相关阅读:
    Hash工具下载地址
    微信聊天记录查看器(程序+源码)
    XEN的启动信息输出到“Platform timer is 14.318MHz HPET”就暂停接收的解决办法
    利用 Serial Over Lan(SOL)搭建 XEN 的调试信息输出环境
    Windows内核开发中如何区分文件对象究竟是文件还是文件夹?
    白盒密码入门
    选择Asp for javascript,非.net。
    8个你至少应该参加一次的极客盛会
    程序员的四种类型
    超棒的30款JS类库和工具
  • 原文地址:https://www.cnblogs.com/huhunet/p/5976159.html
Copyright © 2011-2022 走看看