以下のコードを検討してください。完全に有効な C# コードのように見えますよね?
//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
};
}
}
'Delegate 'Action' does not take 0 arguments.' というコンパイラ エラーが発生します。(Microsoft) C# 4.0 コンパイラを使用して、示された位置で。このエラーが発生するには、別のプロジェクトで ActionSurrogate を宣言する必要があることに注意してください。
もっと面白くなります:
// 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# コンパイラのバグに遭遇しましたか?
これは、ラムダの使用が好きで、将来の使用のためにデータ構造ライブラリを作成しようとしている人にとってはかなり厄介なバグであることに注意してください... (私)
編集:誤ったケースを削除しました。
これを実現するために、元のプロジェクトを最小限にコピーして削除しました。これは文字通り、私の新しいプロジェクトのすべてのコードです。