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

时间:2014-07-29 17:00:54

标签: c# reflection system.reflection aop methodinfo

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中获取受保护的覆盖方法?

1 个答案:

答案 0 :(得分:11)

调用接受GetMethods的{​​{1}}的重载。尝试这样的事情:

BindingFlags

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