如何用反射获取受保护的内部方法

问题描述 投票:4回答:1
public abstract class BaseAspectAttribute : Attribute
{    
    protected internal virtual void OnMethodBeforeExecuting(object args)
    {
        Console.WriteLine("Base Attribute OnMethodBeforeExecuting Work");
    }
}

public class LogAttribute : BaseAspectAttribute
{
    protected override void OnMethodBeforeExecuting(object args)
    {
        Console.WriteLine("Log Attribute OnMethodBeforeExecuting Work");
    }
}

我尝试在LogAttribute =>中获取方法

object[] customAttributesOnMethod  = methodInfo.GetCustomAttributes(typeof (BaseAspectAttribute), true);
foreach (object attribute in customAttributesOnMethod)
{
    MethodInfo[] methodsInSelectedAttribute = attribute.GetType().GetMethods();
}

如何在LogAttribute中获取受保护的覆盖方法?

c# reflection system.reflection aop methodinfo
1个回答
11
投票

调用接受GetMethodsBindingFlags的重载。尝试这样的事情:

attribute.GetType().GetMethods(BindingFlags.Instance | BindingFlags.NonPublic);

http://msdn.microsoft.com/en-us/library/4d848zkb.aspx

© www.soinside.com 2019 - 2024. All rights reserved.