How to gets protected internal methods with reflection
Asked Answered
R

1

9
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?

Rosenkrantz answered 29/7, 2014 at 17:0 Comment(0)
T
18

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

Timber answered 29/7, 2014 at 17:3 Comment(1)
Thanks :) I tried other bindingFlags technics but I didn't use BindingFlags.Instance :/Rosenkrantz

© 2022 - 2024 — McMap. All rights reserved.