private void StringAction(string aString) // method to be called
{
return;
}
private void TestDelegateStatement1() // doesn't work
{
var stringAction = new System.Action(StringAction("a string"));
// Error: "Method expected"
}
private void TestDelegateStatement2() // doesn't work
{
var stringAction = new System.Action(param => StringAction("a string"));
// Error: "System.Argument doesn't take 1 arguments"
stringAction();
}
private void TestDelegateStatement3() // this is ok
{
var stringAction = new System.Action(StringActionCaller);
stringAction();
}
private void StringActionCaller()
{
StringAction("a string");
}
TestDelegateStatement3
作品が、TestDelegateStatement1
が失敗した理由を私は理解していない表現します。いずれの場合も、Action
には、ゼロパラメータを取る方法が用意されています。彼らは(aString
)を取るメソッドを呼び出すかもしれませんが、それは無関係でなければなりません。彼らはパラメータを取らない。これはラムダ式ではできませんか?何か間違っていますか?混乱が
@Botz:分だから、
new Action<string>(myStringParam => StringAction(myStringParam));
、あなたのケースでは、完全なコードは次のようになります。 (コンパイラは 'var'が' System.Action 'であることを知るのに十分な情報がありません) ')。 – devuxer
ああ、ありがとう。これを修正しました。 – Botz3000