如果委托定义放在另一个项目中,编译失败? [英] Compilation fails if delegate definitions is put in another project?
问题描述
更新:我已提交为在Microsoft Connect上的问题,如果你可以重现这个和/或希望看到这个固定的请帮助投票在那里的问题。
我一直在试图解决这个问题几个小时。
真的会感谢你的想法/建议
首先,我有3个文件 Class.cs
Definitions.cs
和 Program.cs
。我已在 http://pastie.org/1049492 上粘贴文件内容,以便您试用。 / p>
问题是,如果您在同一控制台应用程序项目中有所有3个文件。
但是,我有 Class.cs
code> Definitions.cs 在一个库项目中,它是从主控制台应用程序项目引用的,只有 Program.cs
文件,编译失败:
- 委托
Act
不接受2个参数。 - 无法将lambda表达式转换为委托类型DC.Lib.Produce,因为块中的某些返回类型不能隐式转换为委托返回类型...
这里是一个包含3个项目的完整解决方案 - 1个将所有文件组合在一起,另一个将定义放在另一个项目中:
http://dl.dropbox.com/u/149124/DummyConsole.zip
我正在使用VS2010 RTW专业版。
p>有趣。我认为您在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(输入,这个字符串应该出现。
}
}
演示编译为一个程序集,
> csc Test.cs Definitions.cs
演示编译为两个错误的程序集:
> csc / target:library Definitions.cs
> csc Test.cs /r:Definitions.dll
Test.cs(5,43):error CS1662:无法将lambda表达式
转换为委托类型'Produce< string>'
因为块中的一些返回类型不是
可隐式转换为委托返回类型
Test.cs(5,52):error CS1593:Delegate'Bar'不接受2个参数
我不能想到任何 ,一切都是公开的。除了 internal
的原因,该规范很少谈论程序集边界。
有趣的是,我得到相同的错误C#3和4编译器。
电子邮件发送Eric和Mads现在...
可以使用显式参数列表解决这个问题。例如,在我的示例代码,这将工作:
生产< string& produce =
(Bar< string> next)=> input => next(输入,这个字符串应该出现。
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.
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.
The problem is that, If you have ALL 3 files in the same console application project. The application compiles and runs just fine.
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:
- 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 ...
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
I'm using VS2010 RTW Professional edition.
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.
Interestingly, I get the same error for both the C# 3 and 4 compilers.
Emailing Eric and Mads now...
EDIT: 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屋!