次のようなかなりの数のパラメーターを取るデリゲートがあります。
public delegate void MyDelegate(float thereAre, int lotsOf, string parametersIn, int thisDelegate);
public MyDelegate theDelegateInstance;
Visual Studio 2010 には、メソッドがデリゲート シグネチャと一致するのを支援するオート コンプリート機能がないため、これは非常に面倒です。私は基本的に、デリゲートのパラメーターの一部のみを (またはまったく) 取り、他のパラメーターを無視するメソッドを作成できるようにしたいと考えています。
theDelegateInstance += delegate()
{
Debug.Log("theDelegateInstance was called");
};
または
theDelegateInstance += delegate(float thereAre, int lotsOf)
{
if(thereAre > lotsOf) Debug.Log("thereAre is way too high");
};
メソッドがデリゲートを取り、それを次のように呼び出す MyDelegate を返すようにできることがわかりました。
public delegate void VoidMethod();
public static MyDelegate ConvertToMyDelegate(VoidMethod method)
{
return delegate(float thereAre, int lotsOf, string parametersIn, int thisDelegate)
{
method();
};
}
しかし、そのためには、異なる変換ごとに静的メソッドを宣言する必要があります。
目的の結果を得るために、パラメーターなしで最初の例を実行できることがわかりました。
theDelegateInstance += delegate//Notice that there are no brackets here.
{
Debug.Log("theDelegateInstance was called");
};
ただし、これはパラメーターを取らないインライン メソッドでのみ機能します。2 番目の例のように、パラメーターを 1 つでも使用したい場合は、すべてのパラメーターが必要になります。