MethodInfo to C#中的Action

时间:2016-11-09 07:15:54

标签: c# reflection

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

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()
    {

    }
}

如何判断Program.List包含Action1 by Reflection?

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

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

它将打印“True”;

1 个答案:

答案 0 :(得分:1)

试试这个:

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