【发布时间】:2013-09-03 12:33:02
【问题描述】:
在程序集MyLibrary.Common 中,我定义了一个通用委托类型:
namespace MyLibrary.Common {
public delegate TResult Instruction<in TArgument, out TResult>(
CancellationToken cancellationToken,
Action reportProgress,
TArgument argument);
}
然后我通过链接到相应的 DLL 在另一个 VS2010 项目中引用这个程序集。
当我想使用以下方法创建此类型的实例时,我收到以下错误:
Instruction<string, bool> instruction =
(cancellationToken, reportProgress, argument) => SomeOperation(argument);
private static bool SomeOperation(string arg) {
return false;
}
错误我在instruction = ...线上得到的是
无法将源类型“lambda 表达式”转换为目标类型“MyLibrary.Common.Instruction”
当我尝试将SomeOperation(argument) 的应用程序编写为private static bool SomeOperationWrapped(string argument) 并将SomeOperationWrapped 标识符分配给我的instruction 变量时,我得到的错误是
期望一个带有 '??? 的方法SomeOperationWrapped()' 签名
奇怪的是,在另一个 VS2010 项目中,将 lambda 表达式分配给我的 Instruction<TArgument, TResult 变量时没有遇到任何问题。
【问题讨论】:
-
奇怪,对我来说效果很好。
-
您是否尝试过将匿名委托转换为所需的类型: Instruction
instruction = (Instruction )((cancellationToken, reportProgress, argument) => SomeOperation(argument) ); -
试试
Instruction<string, bool> instruction = new Instruction<string, bool>((cancellationToken, reportProgress, argument) => SomeOperation(argument));
标签: c# delegates implicit-conversion implicit-typing