小编Mar*_*tin的帖子

将 Func 动态转换为对应的 Action

我正在尝试对函数和动作使用 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)

c# delegates action func

2
推荐指数
1
解决办法
3321
查看次数

标签 统计

action ×1

c# ×1

delegates ×1

func ×1