【问题标题】:Get custom attribute on method from Castle Windsor interceptor从 Castle Windsor 拦截器获取方法的自定义属性
【发布时间】:2015-10-09 19:03:22
【问题描述】:

我正在尝试访问应用于城堡拦截器中的方法的自定义属性,但方法 Attribute.GetCustomAttribute() 返回 null。

public class MyIntecept : Castle.DynamicProxy.IInterceptor
{
    public void Intercept(IInvocation invocation)
    {
       // myAttr is null. 
       var myAttr = (MyAttribute)Attribute.GetCustomAttribute(
            invocation.Method, typeof(MyAttribute));   
    }
}

[AttributeUsage(AttributeTargets.All, Inherited = true, AllowMultiple = true)]
public class MyAttribute : Attribute
{
    readonly string _value;

    public MyAttribute(string value)
    {
        this._value = value;
    }

    public string Value
    {
        get { return this._value; }
    }
}

public interface IMyInterface
{
    void Do();
}

public class MyClass : IMyInterface
{
    [MyAttribute("MyValue")]
    public void Do()
    {
        Console.WriteLine("Do");
    }
}

如何获得“我的属性”?

附:我正在使用 Castle.Core 3.3.3

【问题讨论】:

    标签: interceptor castle-dynamicproxy castle-windsor-3


    【解决方案1】:

    将属性“MyAttribute”放在接口内部而不是类内部的方法上

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-26
      • 2010-12-12
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 1970-01-01
      • 2016-07-05
      • 2012-02-02
      相关资源
      最近更新 更多