【问题标题】:How to gets protected internal methods with reflection如何通过反射获取受保护的内部方法
【发布时间】:2014-09-21 03:48:54
【问题描述】:
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】:

    调用接受BindingFlagsGetMethods 的重载。试试这样的:

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

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

    【讨论】:

    • 谢谢 :) 我尝试了其他 bindingFlags 技术,但我没有使用 BindingFlags.Instance :/
    猜你喜欢
    • 2013-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多