简体   繁体   中英

How to gets protected internal methods with reflection

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

I try get methods in LogAttribute =>

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

How to gets protected override methods in LogAttribute?

Call the overload of GetMethods which accepts BindingFlags . Try something like this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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