1

I am trying to access a custom attribute applied to a method within a castle interceptor, but method Attribute.GetCustomAttribute() return 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");
    }
}

How can i get 'MyAttribute'?

P.S. I'am using Castle.Core 3.3.3

sp7
  • 246
  • 3
  • 12

1 Answers1

0

Put the attribute "MyAttribute" on the method inside the interface and not inside the class

hugo
  • 1,829
  • 1
  • 18
  • 47