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);