如果委托定义放在另一个项目编译失败? [英] Compilation fails if delegate definitions is put in another project?
问题描述
更新:我提起这个作为的微软连接如果你能重现此和/或希望能看到这个固定请帮忙投票了问题那边。
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.cs
和的Program.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.cs
和 Definitions.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:
- 代表
法
用不了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个项目的完整解决方案 - 1与放在其他项目的定义结合在一起,另外所有文件:结果
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屋!