Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/300.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 接受委托函数的扩展方法_C#_Delegates_Extension Methods - Fatal编程技术网

C# 接受委托函数的扩展方法

C# 接受委托函数的扩展方法,c#,delegates,extension-methods,C#,Delegates,Extension Methods,我仍在努力研究委托函数和扩展方法。我已经为DropDownList创建了一个扩展方法。我想传递要在扩展方法中调用的函数,但得到一个错误参数类型“IOrderedEnumerable”不可分配给参数类型“System.Func” getDropDownDataSource签名 protected IOrderedEnumerable<KeyValuePair<string,string>> getDropDownDataSource() { StateInfoXml

我仍在努力研究委托函数和扩展方法。我已经为DropDownList创建了一个扩展方法。我想传递要在扩展方法中调用的函数,但得到一个错误参数类型“IOrderedEnumerable”不可分配给参数类型“System.Func”

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);
}
调用时,应删除after getDropDownDataSource:

myDropDownList.populateDropDownList(getDropDownDataSource);
编辑:方法组可以隐式转换为具有兼容签名的委托。在本例中,getDropDownDataSource与Func的签名匹配,因此编译器将为您应用转换,有效地执行以下操作

Func<IOrderedEnumerable<KeyValuePair<string,string>>> func = getDropDownDataSource;
myDropDownList.populateDropDownList(func);
调用时,应删除after getDropDownDataSource:

myDropDownList.populateDropDownList(getDropDownDataSource);
编辑:方法组可以隐式转换为具有兼容签名的委托。在本例中,getDropDownDataSource与Func的签名匹配,因此编译器将为您应用转换,有效地执行以下操作

Func<IOrderedEnumerable<KeyValuePair<string,string>>> func = getDropDownDataSource;
myDropDownList.populateDropDownList(func);
是,在myDropDownList.PopulateDropDownList GetDropDownDataSource行中;您正在调用getDropDownDataSource,它返回IOrderedEnumerable。因此,编译器的意思是不能将IOrderedEnumerable转换为Func。要传递Func,您可以删除括号,以便传递类似于myDropDownList.PopulateDropDownList GetDropDownDataSource的指针;或者您可以直接传递数据源代码:

myDropDownList.populateDropDownList(() => {
    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);
}
但这有点难看-

是,在myDropDownList.PopulatedRopDownList GetDropDownDataSource行中;您正在调用getDropDownDataSource,它返回IOrderedEnumerable。因此,编译器的意思是不能将IOrderedEnumerable转换为Func。要传递Func,您可以删除括号,以便传递类似于myDropDownList.PopulateDropDownList GetDropDownDataSource的指针;或者您可以直接传递数据源代码:

myDropDownList.populateDropDownList(() => {
    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);
}

但这有点难看-

并可能将其添加到source.DataSource=delegateAction,使其成为source.DataSource=delegateAction,或使用lamba语法myDropDownList.populateDropDownList=>getdropdownlatasource;你能解释一下为什么会有不同吗?编译器如何将myDropDownList.populateDropDownListgetDropDownDataSource解释为myDropDownList.populateDropDownListgetDropDownDataSource。请在回答中加上这个,谢谢:一个是方法组转换为委托,一个是方法调用@asawyer好的,你是说一个是委托,另一个试图直接进行方法调用?并可能将其添加到source.DataSource=delegateAction使其成为source.DataSource=delegateAction或使用lamba语法myDropDownList.populateDropDownList=>getDropDownDataSource;你能解释一下为什么会有不同吗?编译器如何将myDropDownList.populateDropDownListgetDropDownDataSource解释为myDropDownList.populateDropDownListgetDropDownDataSource。请在回答中加上这个,谢谢:一个是方法组转换为委托,一个是方法调用@asawyer好的,你是说一个是委托,另一个是直接进行方法调用?