Use Delegate.CreateDelegate:
// Static method
Action action = (Action) Delegate.CreateDelegate(typeof(Action), method);
// Instance method (on "target")
Action action = (Action) Delegate.CreateDelegate(typeof(Action), target, method);
For an Action<T>
etc, just specify the appropriate delegate type everywhere.
In .NET Core, Delegate.CreateDelegate
doesn't exist, but MethodInfo.CreateDelegate
does:
// Static method
Action action = (Action) method.CreateDelegate(typeof(Action));
// Instance method (on "target")
Action action = (Action) method.CreateDelegate(typeof(Action), target);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…