引子
delegate:代表,授权,翻译为“委托”,即用对象代表方法成员或对象被授权执行方法成员。看下面一小段代码:
{
return x>y?x:y;
}
int Min(int x,int y)
{
return x<y?x:y;
}
上面两个函数的共同特点:具有相同的返回值和参数列表。在C++里,我们使用函数指针来指代(被授权,代表)这两个函数。实际上,我们可以用函数指针指向任意一个具有相同返回值和参数列表的函数(静态方法或实例的方法成员)。
int (*p)(int,int);
//让指针p指向Max函数
p=max;
//利用指针调用Max
c=(*p)(5,6);
在C#里没有提供函数指针,取而代之的是委托(delegate);利用委托,我们可以像使用函数指针一样在程序运行时动态指向具备相同签名(具有相同参数类型、参数个数以及相同类型返回值)的方法。
委托的定义
之前我们在引出委托时已经简单的介绍了委托的本质:函数指针。说的通俗一些,委托就是能够让方法作为变量来传递。我个人喜欢下面这个定义:
委托是一种类型安全的函数回调机制, 它不仅能够调用实例方法,也能调用静态方法,并且具备按顺序执行多个方法的能力。
也就是说,委托可以在程序运行时调用不同方法函数,只要这个方法签名和委托签名保持一致。与函数指针不同的是,委托是类型安全的。所谓类型安全,是指在编译时编译器会检测委托对象的签名是否委托声明一致。看下面一小段简单的代码感受委托的含义:
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateSamples
{
//声明一个委托,参数为string,无返回值
delegate void DelSamples(string msg);
class Program
{
static void Main(string[] args)
{
DelSamples delSample = new Program().SpeakChinese; //调用实例方法
delSample += SpeakEnglish; //调用静态方法
delSample("KoalaStudio");
Console.ReadKey();
}
private void SpeakChinese(string msg)
{
Console.WriteLine("你好,我是{0}",msg);
}
private static void SpeakEnglish(string msg)
{
Console.WriteLine("Hello,I'm {0}",msg);
}
}
}
输出结果:
委托的声明
简单委托
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DelegateSamples
{
//声明一个委托,参数为string,无返回值
delegate void DelSamples(string msg);
class Program
{
static void Main(string[] args)
{
//使用new关键字
DelSamples delSample = new DelSamples(new Program().SpeakChinese);
delSample("Koala工作室");
//不使用new,自动推断委托类型
DelSamples delSample2 = SpeakEnglish;
delSample2("KoalaStudio");
//利用Lambda表达式
DelSamples delSample3 = (string msg) => SpeakEnglish(msg);
delSample3(
在WebForm时代,CheckBoxList和RadioButtonList都非常容易实现。不得不承认,这两个控件还是非常实用的。
但是在MVC中并没有相关的支持,可能微软觉得没必要了吧,不过真的有人讲这个功能完成了。
代码摘抄自狼奔代码生成器。地址是:http://www.cnblogs.com/langben 有兴趣的可以研究下。
这个主要是为了收集代码。。
CheckBoxListHelper:
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using System.Linq.Expressions;
namespace Models
{
public static class CheckBoxListHelper
{
/// <summary>
/// CheckBox列表
/// </summary>
/// <param name="helper">辅助类</param>
/// <param name="name">字段名称</param>
/// <param name="selectList">集合</param>
/// <returns></returns>
public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList)
{
return CheckBoxList(helper, name, selectList, new { });
}
/// <summary>
/// CheckBox列表
/// </summary>
/// <param name="helper">辅助类</param>
/// <param name="name">字段名称</param>
/// <param name="selectList">集合</param>
/// <param name="htmlAttributes">html标签</param>
/// <returns></returns>
public static MvcHtmlString CheckBoxList(this HtmlHelper helper, string name, IEnumerable<SelectListItem> selectList, object htmlAttributes)
{
IDictionary<string, object> HtmlAttributes = HtmlHelper.AnonymousObjectToHtmlAttributes(htmlAttributes);
HashSet<string> set = new HashSet<string>();
List<SelectListItem> list = new List<SelectListItem>();
string selectedValues = Convert.ToString((selectList as SelectList).SelectedValue);
if (!string.IsNullOrEmpty(selectedValues))
{
if (selectedValues.Contains(","))
{
string[] tempStr = selectedValues.Split(',');
for (int i = 0; i < tempStr.Length; i++)
{
set.Add(tempStr[i]);
}
}
else
{
set.Add(selectedValues);
}
}
foreach (SelectListItem item in selectList)
{
item.Selected = (item.Value != null) ? set.Contains(item.Value) : set.Contains(item.Text);
list.Add(item);
}
selectList = list;
HtmlAttributes.Add("type", "checkbox");
HtmlAttributes.Add("id", name);
HtmlAttributes.Add("name", name);
//HtmlAttributes.Add("style", "margin:0 0 0 10px;line-height:30px; vertical-align:-8px;border:none;");
StringBuilder stringBuilder = new StringBuilder();
foreach (SelectListItem selectItem in selectList)
{
IDictionary<string, object> newHtmlAttributes = HtmlAttributes.DeepCopy();
newHtmlAttributes.Add("value", selectItem.Value);
if (selectItem.Selected)
{
newHtmlAttributes.Add("checked", "checked"
写Javascript来判断是否有对RadioButtonList选项选择,效果如下:
准备好RadioButtonList数据源:
Namespace Insus.NET
Public Class Cosmetic
Private _ID As Integer
Private _Type As String
Private _Name As String
Private _Weight As Decimal
Private _UM As String
Public Property ID As Integer
Get
Return _ID
End Get
Set(value As Integer)
_ID = value
End Set
End Property
Public Property Type As String
Get
Return _Type
End Get
Set(value As String)
_Type = value
End Set
End Property
Public Property Name As String
Get
Return _Name
End Get
Set(value As String)
_Name = value
End Set
End Property
Public Property Weight As Decimal
Get
Return _Weight
End Get
Set(value As Decimal)
_Weight = value
End Set
End Property
Public Property UM As String
Get
Return _UM
End Get
Set(value As String)
_UM = value
End Set
End Property
Public Sub New()
End Sub
Public Sub New(id As Integer, type As String, name As String, weight As Decimal, um As String)
Me._ID = id
Me._Type = type
Me._Name = name
Me._Weight = weight
Me._UM = um
End Sub
Public Function GetData() As List(Of Cosmetic)
Dim o As New List(Of Cosmetic)
Dim c As New Cosmetic(1, "滋润霜", "玉兰油", 50, "g")
o.Add(c)
Dim c1 As New Cosmetic(2, "滋润霜", "雅诗兰黛", 100, "g")
o.Add(c1)
Dim c2 As New Cosmetic(3, "滋润霜", " 兰蔻", 80, "g")
o.Add(c2)
Dim c3 As