c# - 如何通过反射获得 protected 内部方法

标签 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 中获取 protected 覆盖方法?

最佳答案

调用 GetMethods 的重载,它接受 BindingFlags。尝试这样的事情:

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

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

关于c# - 如何通过反射获得 protected 内部方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25020966/

相关文章:

c# - 如何在 ASP.Net MVC 中使用 Task 重定向到一个 Action

java - 如何获取.class文件的包名?

.net - 泛型和松耦合 : Can Class<T> be decoupled without type assumptions?

java - 具有动态 ArrayList 项类型的 Gson TypeToken

c# - 列出在反射中创建的类

c# - 无法找到或加载程序集“tmpAssembly,

c# - 如何防止 MemberInfo.IsDefined 在不相关的属性上抛出 FileNotFoundException?

c# - 从 C# 类调用 MVC Controller

c# - 在 C# 中调用 C++ dll 时找不到入口点

c# - 如何通过缩小 c# 和 linq 中的另一个列表来创建列表