c#中的从MethodInfo到Action



我有一个这样的静态列表:

class Program
{
    public static List<Action> List { get; set; } = new List<Action>();
    static void Main(string[] args)
    {
        Test test = new Test();
        test.Work();
        var type = test.GetType();
        var method = type.GetMethod("Action1",System.Reflection.BindingFlags.Instance|System.Reflection.BindingFlags.NonPublic);
        //Console.WriteLine(List.Any(p => p == new Action(method.)));
    }
}
public class Test
{
    public void Work()
    {
        Program.List.Add(new Action(Action1));
    }
    private void Action1()
    {
    }
}

我如何判断程序。列表包含Action1通过反射?

如果Action1方法是公共的,我可以这样做:

Console.WriteLine(List.Any(p => p == new Action(test.Action1)));

打印"True";

试试这个:

Console.WriteLine(List.Any(p => p.Method.MethodHandle.Value == method.MethodHandle.Value));

相关内容

  • 没有找到相关文章

最新更新