“委托”System.Action“不带0的参数。”这是一个C#编译器的bug(lambda表达式+两个项目)? [英] 'Delegate 'System.Action' does not take 0 arguments.' Is this a C# compiler bug (lambdas + two projects)?

查看:578
本文介绍了“委托”System.Action“不带0的参数。”这是一个C#编译器的bug(lambda表达式+两个项目)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

考虑下面的code。看起来完全有效的C#code吧?

Consider the code below. Looks like perfectly valid C# code right?

//Project B
using System;
public delegate void ActionSurrogate(Action addEvent);
//public delegate void ActionSurrogate2();
// Using ActionSurrogate2 instead of System.Action results in the same error
// Using a dummy parameter (Action<double, int>) results in the same error

// Project A
public static class Class1 {
    public static void ThisWontCompile() {
        ActionSurrogate b = (a) =>
                            {
                                a(); // Error given here
                            };
    }
}

我得到一个编译错误'代表'动作'不带0的参数。在使用(微软)C#4.0编译器指示的位置。请注意,您在不同的项目申报ActionSurrogate此错误来体现。

I get a compiler error 'Delegate 'Action' does not take 0 arguments.' at the indicated position using the (Microsoft) C# 4.0 compiler. Note that you have to declare ActionSurrogate in a different project for this error to manifest.

问题就变得更有趣:

// Project A, File 1
public static class Class1 {
    public static void ThisWontCompile() {
        ActionSurrogate b = (a) => { a(); /* Error given here */ };
        ActionSurrogate c = (a) => { a(); /* Error given here too */ };
        Action d = () => { };
        ActionSurrogate c = (a) => { a(); /* No error is given here */ };
    }
}

难道我偶然发现这里C#编译器错误?

Did I stumble upon a C# compiler bug here?

请注意,这是有人使用lambda表达式很多谁喜欢,并试图为未来使用的数据结构库pretty恼人的错误......(我)

Note that this is a pretty annoying bug for someone who likes using lambdas a lot and is trying to create a data structures library for future use... (me)

编辑:删除erronous情况下

removed erronous case.

我复制并剥夺我原来的项目分解到最小,以实现这一目标。这是字面上都在我的新项目code。

I copied and stripped my original project down to the minimum to make this happen. This is literally all the code in my new project.

推荐答案

这可能是与类型inferance,apperently编译器推断 A 为<$ C $问题C>动作&LT; T&GT; 而不是动作(它可能会认为 A ActionSurrogate ,这将符合动作&LT;作用&gt;&GT; 签名)。尝试指定 A 的类型明确的:

This probably is a problem with type inferance, apperently the compiler infers a as an Action<T> instead of Action (it might think a is ActionSurrogate, which would fit the Action<Action>> signature). Try specifying the type of a explicitly:

    ActionSurrogate b = (Action a) =>
                        {
                            a();
                        };

如果不是这种情况 - 可能会检查你的周围项目定义的动作代表采取一个参数任何自我

If this is not the case - might check around your project for any self defined Action delegates taking one parameter.

这篇关于“委托”System.Action“不带0的参数。”这是一个C#编译器的bug(lambda表达式+两个项目)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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