views:

175

answers:

1

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.

+8  A: 

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.");
Jon Skeet
Thanks! Awaiting impatiently for more feedback... Hopefully I don't have to go back to 3.5 just for this T.T
chakrit
@chakrit: I've put in a workaround, but I don't see how going back to .NET 3.5 would help you - as I say, I've seen the same issue with the C# 3 compiler.
Jon Skeet
I'm rewriting/refactoring a library which worked fine in 3.5 to utilize new .NET 4.0 stuff.... I guess I must've tripped up some wires in the process.
chakrit
Note: it works in **Mono**. .NET is converting the code to `delegates` directly while **Mono** stores them using variables and emitted types.
Jaroslav Jandek
@Jaroslav Thanks for the suggestion! I could tear out the problematic parts and compile it on Mono, since it's not dependent on NET40. That'd work. I'll try tomorrow and lets you guys know. Really tired and should be sleeping right now.
chakrit
Any updates from your mails?
chakrit
@chakrit: They're investigating.
Jon Skeet