【发布时间】:2012-10-25 17:42:17
【问题描述】:
我有一组 IEnumerable<School> 正在传递给扩展
填充DropDownList 的方法。我也想通过
DataValueField 和 DataTextField 作为论据,但我希望它们成为
强类型。
基本上,我不想为DataValueField 和DataTextField 参数传递string,这很容易出错。
public static void populateDropDownList<T>(this DropDownList source,
IEnumerable<T> dataSource,
Func<T, string> dataValueField,
Func<T, string> dataTextField) {
source.DataValueField = dataValueField; //<-- this is wrong
source.DataTextField = dataTextField; //<-- this is wrong
source.DataSource = dataSource;
source.DataBind();
}
这样称呼...
myDropDownList.populateDropDownList(states,
school => school.stateCode,
school => school.stateName);
我的问题是,如何将DataValueField 和DataTextField 强类型作为参数传递给populateDropDownList?
【问题讨论】:
-
我不明白这些字段是字符串类型的。
标签: c# lambda strong-typing