我仍在尝试围绕委托函数和扩展方法。我为DropDownList
. 我想传递要在我的扩展方法中调用的函数,但出现错误Argument type 'IOrderedEnumerable<KeyValuePair<string,string>>' is not assignable to parameter type 'System.Func<IOrderedEnumerable<KeyValuePair<string,string>>>'
public static class DropDownListExtensions {
public static void populateDropDownList(this DropDownList source, Func<IOrderedEnumerable<KeyValuePair<string, string>>> delegateAction) {
source.DataValueField = "Key";
source.DataTextField = "Value";
source.DataSource = delegateAction;
source.DataBind();
}
}
被这样称呼……
myDropDownList.populateDropDownList(getDropDownDataSource());
getDropDownDataSource 签名...
protected IOrderedEnumerable<KeyValuePair<string,string>> getDropDownDataSource() {
StateInfoXmlDocument stateInfoXmlDocument = new StateInfoXmlDocument();
string schoolTypeXmlPath = string.Format(STATE_AND_SCHOOL_TYPE_XML_PATH, StateOfInterest, SchoolType);
var nodes = new List<XmlNode>(stateInfoXmlDocument.SelectNodes(schoolTypeXmlPath).Cast<XmlNode>());
return nodes.Distinct().Select(x => new KeyValuePair<string, string>(x.Attributes["area"].Value, x.Attributes["area"].Value)).OrderBy(x => x.Key);
}