C# 将lambda转换为自定义委托

C# 将lambda转换为自定义委托,c#,delegates,implicit-conversion,implicit-typing,C#,Delegates,Implicit Conversion,Implicit Typing,在程序集MyLibrary.Common中,我定义了一个泛型委托类型: namespace MyLibrary.Common { public delegate TResult Instruction<in TArgument, out TResult>( CancellationToken cancellationToken, Action reportProgress, TArgument argument); } Instruction

在程序集
MyLibrary.Common
中,我定义了一个泛型委托类型:

namespace MyLibrary.Common {
  public delegate TResult Instruction<in TArgument, out TResult>(
      CancellationToken cancellationToken,
      Action reportProgress,
      TArgument argument);
}
Instruction<string, bool> instruction = (CancellationToken cancellationToken, Action reportProgress, string argument) => SomeOperation(argument);

Instruction<string, bool> instruction = (Instruction<string, bool>)((CancellationToken cancellationToken, Action reportProgress, string argument) => SomeOperation(argument));
名称空间MyLibrary.Common{
公共委托TResult指令(
CancellationToken CancellationToken,
行动报告进展情况,
目标论点);
}
然后,通过链接到相应的DLL,在另一个VS2010项目中引用此程序集

当我想使用以下方法创建此类型的实例时,我得到以下错误:

Instruction<string, bool> instruction =
  (cancellationToken, reportProgress, argument) => SomeOperation(argument);

private static bool SomeOperation(string arg) {
  return false;
}
指令=
(cancellationToken、reportProgress、argument)=>SomeOperation(argument);
私有静态布尔操作(字符串arg){
返回false;
}
我在
指令=…
行上得到的错误是

无法将源类型“lambda expression”转换为目标类型“MyLibrary.Common.Instruction”


当我尝试将
SomeOperation(argument)
的应用程序编写为
私有静态bool SomeOperationWrapped(string argument)
并将该
SomeOperationWrapped
标识符分配给我的
指令
变量时,我得到的错误是

需要一个带“??”的方法???SomeOperationWrapped()签名



奇怪的是,在另一个VS2010项目中,将lambda表达式分配给我的
指令没有任何问题。有趣的是,您说在另一个VS2010项目中没有此类问题。如果能看到代码(工作正常)是什么样子就好了

源项目和消费项目是否都设置为以相同的.Net Framework版本为目标,并且可能还使用相同的工具版本

可能编译器在推断类型时遇到问题-请尝试显式地将lambda参数类型化,和/或显式地将lambda强制转换为委托类型:

namespace MyLibrary.Common {
  public delegate TResult Instruction<in TArgument, out TResult>(
      CancellationToken cancellationToken,
      Action reportProgress,
      TArgument argument);
}
Instruction<string, bool> instruction = (CancellationToken cancellationToken, Action reportProgress, string argument) => SomeOperation(argument);

Instruction<string, bool> instruction = (Instruction<string, bool>)((CancellationToken cancellationToken, Action reportProgress, string argument) => SomeOperation(argument));
指令指令=(CancellationToken CancellationToken,Action reportProgress,string参数)=>SomeOperation(参数);
指令指令=(指令)((CancellationToken CancellationToken,Action reportProgress,string参数)=>SomeOperation(参数));

有趣的是,您说您在另一个VS2010项目中没有此类问题。如果能看到代码(工作正常)是什么样子就好了

源项目和消费项目是否都设置为以相同的.Net Framework版本为目标,并且可能还使用相同的工具版本

可能编译器在推断类型时遇到问题-请尝试显式地将lambda参数类型化,和/或显式地将lambda强制转换为委托类型:

namespace MyLibrary.Common {
  public delegate TResult Instruction<in TArgument, out TResult>(
      CancellationToken cancellationToken,
      Action reportProgress,
      TArgument argument);
}
Instruction<string, bool> instruction = (CancellationToken cancellationToken, Action reportProgress, string argument) => SomeOperation(argument);

Instruction<string, bool> instruction = (Instruction<string, bool>)((CancellationToken cancellationToken, Action reportProgress, string argument) => SomeOperation(argument));
指令指令=(CancellationToken CancellationToken,Action reportProgress,string参数)=>SomeOperation(参数);
指令指令=(指令)((CancellationToken CancellationToken,Action reportProgress,string参数)=>SomeOperation(参数));
受其启发,原来是自己打开这个麻烦的项目(而不是打开它的父解决方案)解决了问题。
复制修复的步骤:

  • 自己打开项目。VS2010为其创建了一个新的(临时)解决方案
  • 打开项目的原始解决方案以继续处理代码
到目前为止,我还不知道这到底是为什么,但我认为这是一种书外的“清洁项目”操作。

受此启发,结果证明,打开这个麻烦的项目(而不是打开它的父解决方案)可以解决问题。
复制修复的步骤:

  • 自己打开项目。VS2010为其创建了一个新的(临时)解决方案
  • 打开项目的原始解决方案以继续处理代码

到目前为止,我不知道这到底是为什么,但我认为这是一种书外的“清理项目”操作。

奇怪,它对我来说非常好。您是否尝试将匿名委托强制转换为所需的类型:指令指令=(指令)((取消令牌,报告进度,参数)=>某个操作(参数));尝试
Instruction Instruction=new Instruction((cancellationToken,reportProgress,argument)=>SomeOperation(argument));
Strange,对我来说效果非常好。您是否尝试将匿名委托强制转换为所需类型:Instruction Instruction=(Instruction)((cancellationToken,reportProgress,argument)=>SomeOperation(参数));Try
指令指令=新指令((cancellationToken,reportProgress,参数)=>SomeOperation(参数));
你确定引用了正确的DLL吗?听起来你得到了不同版本的MyLibrary.Common。我确定我引用的DLL是正确的。我进行了双重、三重和四重检查。你确定引用了正确的DLL吗?听起来你得到了不同版本的MyLibrary.Common。我确定我引用的DLL是正确的。我检查了两倍、三倍和四倍