2015-07-20 149 views
1

我試圖訪問應用於城堡攔截器內的方法的自定義屬性,但方法Attribute.GetCustomAttribute()返回null。從Castle Windsor攔截器獲取自定義屬性方法

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

我怎樣才能得到'MyAttribute'?

P.S.我正在使用Castle.Core 3.3.3

回答

0

將屬性「MyAttribute」放在界面內的方法中,而不是類內

相關問題