如果将委托定义放在另一个项目中,编译会失败吗? [英] Compilation fails if delegate definitions is put in another project?

查看:32
本文介绍了如果将委托定义放在另一个项目中,编译会失败吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:我已将此提交为 Microsoft Connect 上的一个问题 如果您可以重现此问题和/或希望看到此问题得到修复,请帮助对该问题进行投票.

UPDATE: I've filed this as an issue on Microsoft Connect if you can reproduce this and/or would love to see this fixed please help vote up the issue over there.

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

I've been trying to solve this problem for hours now.
Would really appreciate whatever idea/advice you can think of.

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

First of all, I have 3 files Class.cs Definitions.cs and Program.cs. I've pasted file contents over at http://pastie.org/1049492 for you to try out.

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

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

If however, I have Class.cs and Definitions.cs in a "library" project which is referenced to from the main console application project which has only the Program.cs file, compilation fails with:

  • 委托 Act 不接受 2 个参数.
  • 无法将 lambda 表达式转换为委托类型DC.Lib.Produce",因为块中的某些返回类型不能隐式转换为委托返回类型...
  • Delegate Act does not take 2 arguments.
  • Cannot convert lambda expression to delegate type 'DC.Lib.Produce' because some of the return types in the block are not implicitly convertible to the delegate return type ...

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

Here is a complete solution with 3 projects -- 1 with all files combined together and another with the definitions put in another project:
http://dl.dropbox.com/u/149124/DummyConsole.zip

我使用的是 VS2010 RTW 专业版.

I'm using VS2010 RTW Professional edition.

推荐答案

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

Interesting. I think you've found an actual bug in the C# compiler - although I may be missing something subtle. I've written a slightly simplified version which avoids possibilities of overloading etc coming into play, and which dispenses with the extra method:

// 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.");
    }    
}

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

Demonstration of compiling as one assembly, with no errors:

> csc Test.cs Definitions.cs

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

Demonstration of compiling as two assemblies with errors:

> 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

我想不出任何为什么这应该在不同的程序集中有所不同,因为一切都是公开的.除了内部原因外,规范很少讨论程序集边界.

I can't think of any reason why this should be different across different assemblies, as everything's public. The spec rarely talks about assembly boundaries other than for internal reasons.

有趣的是,我在 C# 3 和 4 编译器中遇到了相同的错误.

Interestingly, I get the same error for both the C# 3 and 4 compilers.

现在给 Eric 和 Mads 发电子邮件...

Emailing Eric and Mads now...

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

Note that you can work around this using an explicit parameter list. For example, in my sample code, this will work:

Produce<string> produce =
    (Bar<string> next) => input => next(input, "This string should appear.");

这篇关于如果将委托定义放在另一个项目中,编译会失败吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆