小编Joa*_*nna的帖子

行动代表.如何在委托中调用方法信息?

我需要为在Action委托中调用的方法获取MethodInfo以便检查Action中调用的方法是否具有MyCustomAttibute

    public void Foo( Action action )
    {
        if(Attribute.GetCustomAttributes(action.Method, typeof(MyCustomAttribute)).Count() == 0)
        {
            throw new ArgumentException("Invalid action");
        }
    }
Run Code Online (Sandbox Code Playgroud)

应该能够调用Foo方法如下:

    Foo(() =>
    {
            instanceOfFooClass.Method1().Method2();
    });
Run Code Online (Sandbox Code Playgroud)

在Foo方法中,我想确保Method1和Method2具有MyCustomAttribute.但是action.Method给了我MethodInfo,这是委托的动作,当使用lambda表达式时会发生.有没有办法获得Method1和Method2 MethodInfo?

c# delegates

8
推荐指数
1
解决办法
1655
查看次数

标签 统计

c# ×1

delegates ×1