繁体   English   中英

MethodInfo to C#中的Action

[英]MethodInfo to Action in C#

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

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)));

它将打印“真实”;

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM