代码之家  ›  专栏  ›  技术社区  ›  chakrit Dutchie432

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

  •  6
  • chakrit Dutchie432  · 技术社区  · 14 年前

    我把这个作为 an issue on Microsoft Connect 如果你能复制这个和/或想看到这个固定请帮助投票的问题在那里。


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

    首先,我有3个文件 Class.cs Definitions.cs Program.cs http://pastie.org/1049492 让你试试。

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

    定义.cs 在从主控制台应用程序项目中引用的“库”项目中,该项目只有 程序.cs 文件,编译失败:

    • Act

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

    我使用的是VS2010 RTW专业版。

    2 回复  |  直到 14 年前
        1
  •  8
  •   Jon Skeet    14 年前

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

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

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

    > csc Test.cs Definitions.cs
    

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

    > 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
    

    我想不起来 任何 为什么这在不同的程序集之间应该是不同的,因为一切都是公共的。规范很少讨论组件边界,除了 internal 原因。

    有趣的是,对于C#3和4编译器,我都得到了相同的错误。

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

    Produce<string> produce =
        (Bar<string> next) => input => next(input, "This string should appear.");
    
        2
  •  1
  •   iastg    13 年前

    我通过重命名 C:\Windows\Microsoft.NET\Framework\v4.0.30319\Microsoft.CSharp.targets(不要删除它!)