MethodInfo からアクションデリゲートを作成するにはどうすればよいでしょうか? 質問する

MethodInfo からアクションデリゲートを作成するにはどうすればよいでしょうか? 質問する

MethodInfo オブジェクトからアクション デリゲートを取得したいのですが、これは可能ですか?

ベストアンサー1

使用デリゲート.デリゲートの作成:

// Static method
Action action = (Action) Delegate.CreateDelegate(typeof(Action), method);

// Instance method (on "target")
Action action = (Action) Delegate.CreateDelegate(typeof(Action), target, method);

などについてはAction<T>、どこでも適切なデリゲート タイプを指定するだけです。

.NET Core にはDelegate.CreateDelegate存在しませんが、MethodInfo.CreateDelegate次のものがあります:

// Static method
Action action = (Action) method.CreateDelegate(typeof(Action));

// Instance method (on "target")
Action action = (Action) method.CreateDelegate(typeof(Action), target);

おすすめ記事