如果将委托定义放在另一个项目中,则编译失败?

cha*_*rit 6 c# compiler-errors visual-studio-2010

更新:我已将此作为Microsoft Connect的问题提交,如果您可以重现这一点和/或希望看到此修复,请帮助在那里投票解决问题.


我一直试图解决这个问题几个小时了.
非常感谢您能想到的任何想法/建议.

首先,我有3个文件Class.cs Definitions.csProgram.cs.我已经在http://pastie.org/1049492粘贴了文件内容供您试用.

问题是,如果在同一个控制台应用程序项目中有所有3个文件.应用程序编译并运行得很好.

但是,如果我有Class.csDefinitions.cs在从它只有在主控制台应用程序项目中引用到"库"的项目Program.cs文件,编译失败:

  • 代表Act不接受2个论点.
  • 无法将lambda表达式转换为委托类型'DC.Lib.Produce',因为块中的某些返回类型不能隐式转换为委托返回类型...

这是一个包含3个项目的完整解决方案 - 其中1个包含所有文件,另一个包含在另一个项目中的定义:http:
//dl.dropbox.com/u/149124/DummyConsole.zip

我正在使用VS2010 RTW专业版.

Jon*_*eet 8

有趣.我认为你在C#编译器中发现了一个实际的错误 - 虽然我可能会遗漏一些微妙的东西.我写了一个稍微简化的版本,避免了重载等的可能性,并且省去了额外的方法:

// Definitions.cs
public interface IData { }
public delegate IData Foo(IData input);
public delegate IData Bar<T>(IData input, T extraInfo);
public delegate Foo Produce<T>(Bar<T> next);

// Test.cs
class Test
{
    static void Main()
    {
        Produce<string> produce = 
            next => input => next(input, "This string should appear.");
    }    
}
Run Code Online (Sandbox Code Playgroud)

演示编译为一个程序集,没有错误:

> csc Test.cs Definitions.cs
Run Code Online (Sandbox Code Playgroud)

演示编译为具有错误的两个程序集:

> csc /target:library Definitions.cs
> csc Test.cs /r:Definitions.dll

Test.cs(5,43): error CS1662: Cannot convert lambda expression 
        to delegate type 'Produce<string>'
        because some of the return types in the block are not 
        implicitly convertible to the delegate return type
Test.cs(5,52): error CS1593: Delegate 'Bar' does not take 2 arguments
Run Code Online (Sandbox Code Playgroud)

我想不出有什么理由为什么在不同的集会中这应该是不同的,因为一切都是公开的.除了internal原因之外,规范很少讨论装配边界.

有趣的是,我对C#3和4编译器都有同样的错误.

现在通过电子邮件发送Eric和Mads ......

编辑:请注意,您可以使用显式参数列表解决此问题.例如,在我的示例代码中,这将起作用:

Produce<string> produce =
    (Bar<string> next) => input => next(input, "This string should appear.");
Run Code Online (Sandbox Code Playgroud)

  • 注意:它适用于**Mono**..NET正在将代码直接转换为`delegates`,而**Mono**使用变量和发出的类型存储它们. (2认同)