Method attribute access from Castle Windsor interceptor

I am trying to access a custom attribute applied to a method inside a lock interceptor, for example:

[MyCustomAttribute(SomeParam = "attributeValue")] public virtual MyEntity Entity { get; set; } 

using the following code:

 internal class MyInterceptor : IInterceptor { public void Intercept(IInvocation invocation) { if (invocation.Method.GetCustomAttributes(typeof(MyCustomAttribute), true) != null) { //Do something } } } 

The interceptor fires OK when the method is called, but this code does not return a user attribute. How can I achieve this?

+4
source share
3 answers

I think I understood this because of the difference between the property and the method. This is the get_ method that fires the interceptor, and it is not decorated with the attribute of the parent property.

+1
source

Try the Attribute.GetCustomAttribute(...) static method for this. This is strange, but these two methods sometimes give different results for some strange reason.

+4
source

Try

 private static Attribute getMyCustomAttribute(IInvocation invocation) { var methodInfo = invocation.MethodInvocationTarget; if (methodInfo == null) { methodInfo = invocation.Method; } return Attribute.GetCustomAttribute(methodInfo, typeof(MyCustomAttribute), true); } 
+3
source

Source: https://habr.com/ru/post/1305432/


All Articles