如何使用特定签名过滤Type.GetMethods的结果?

时间:2014-05-13 02:45:20

标签: c# reflection

我想过滤具有我班级中定义的特定签名的方法。请参阅下面的代码,

 public class A {
    delegate float op(int from, int to);
    List<op> operations;
    public A () {
        MethodInfo[] methods = this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        // filter goes here.
        // foreach method which is op
        // operations.Add(m)
    }
    private float operation1(int from, int to) {
        return 1;
    }

    private float operation2(int from, int to) {
        return 2;
    }


    public int other2() {
        return -1;
    }
}

怎么做?

MethodInfo[] methods = this.GetType().GetMethods(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly);
        foreach (MethodInfo m in methods)
        {
            try
            {
                op operation = (op)Delegate.CreateDelegate(typeof(op), this, m);
                operations.Add(operation);
            }
            catch (Exception)
            {
                // nothing except logging
            }
        }

0 个答案:

没有答案