Intercept Properties With Castle Windsor IInterceptor

Posted by jeffn825 on Stack Overflow See other posts from Stack Overflow or by jeffn825
Published on 2010-06-02T16:57:58Z Indexed on 2010/06/02 23:24 UTC
Read the original article Hit count: 304

Does anyone have a suggestion on a better way to intercept a properties with Castle DynamicProxy? Specifcally, I need the PropertyInfo that I'm intercepting, but it's not directly on the IInvocation, so what I do is:

        public static PropertyInfo GetProperty(this MethodInfo method)
    {
        bool takesArg = method.GetParameters().Length == 1;
        bool hasReturn = method.ReturnType != typeof(void);
        if (takesArg == hasReturn) return null;
        if (takesArg)
        {
            return method.DeclaringType.GetProperties()
                .Where(prop => prop.GetSetMethod() == method).FirstOrDefault();
        }
        else
        {
            return method.DeclaringType.GetProperties()
                .Where(prop => prop.GetGetMethod() == method).FirstOrDefault();
        }
    }

Then in my IInterceptor:

  #region IInterceptor Members

    public void Intercept(IInvocation invocation)
    {
        bool doSomething =                                 invocation.Method.GetProperty().GetCustomAttributes(true).OfType<SomeAttribute>().Count() > 0;

    }

    #endregion

Thanks.

© Stack Overflow or respective owner

Related posts about castle-windsor

Related posts about dynamic-proxy