[英]Execute a C# Lambda expression inside a Javascript function with Parameter
[英]Embedded C# lambda expression as function parameter
有誰知道下面的代碼有什么問題,這些代碼無法在VS2013中編譯?
GenericCommand.AddHandlerFactory("MyKey", (cmd, action) =>
{
return (command) =>
{
var result = new SuccessResult() { ResultText = "some example text" };
result.Send(command.Configuration);
};
});
AddHandlerFactory的原型是:
public static void AddHandlerFactory(string key, Func<GenericCommand, Action> handlerFactory)
在VS2013中編譯時,它顯示
不能在此范圍內聲明名為command的局部變量,因為它將賦予command .... ...不同的含義。
和
委托System.Func WindowsPhoneTestFramework.Client.AutomationClient.Remote.GenericCommand,System.Action不帶2個參數
源代碼的更多詳細信息位於: https : //github.com/Expensify/WindowsPhoneTestFramework/blob/master/Client/AutomationClient/Remote/GenericCommand.cs
錯誤消息為:
無法將lambda表達式轉換為委托類型委托System.Func WindowsPhoneTestFramework.Client.AutomationClient.Remote.GenericCommand,System.Action,因為塊中的某些返回類型不能隱式轉換為委托返回類型。
您有兩個共享相同名稱的參數:
(command, action) =>
是帶有參數命名command
一項操作
return (command) =>
是另一個動作,帶有另一個參數命名command
因此,您必須重命名兩個參數名稱之一。
正如@Dirk解釋的那樣,您返回Action<T>
而不是Action
。 因此,您可以嘗試以下操作:
GenericCommand.AddHandlerFactory("MyKey", (cmd, action) =>
{
return () =>
{
var result = new SuccessResult() { ResultText = "some example text" };
result.Send(cmd.Configuration);
};
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.