我正在尝试对函数和动作使用 Convert 方法,因此我可以避免编写接受 Func 类型委托的重复方法。Convert 方法来自Convert Action<T> 到 Action<object>
public class Program
{
static void Main(string[] args)
{
var program = new Program();
var mi = program.GetType().GetMethod("Function", BindingFlags.Instance | BindingFlags.Public);
// Can be any version of Func
var funcType = typeof(Func<int, int>);
// Create action delegate somehow instead
var del = mi.CreateDelegate(funcType, null);
// Or dynamically convert the Func to a corresponding Action type (in this case Action<int>)
}
// Or find a way to pass it in as …Run Code Online (Sandbox Code Playgroud)