简体   繁体   English

拦截城堡温莎IInterceptor的属性

[英]Intercept Properties With Castle Windsor IInterceptor

Does anyone have a suggestion on a better way to intercept a properties with Castle DynamicProxy? 有没有人建议用更好的方法拦截Castle DynamicProxy的属性?

Specifically, I need the PropertyInfo that I'm intercepting, but it's not directly on the IInvocation, so what I do is: 具体来说,我需要我正在拦截的PropertyInfo,但它不是直接在IInvocation上,所以我所做的是:

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: 然后在我的IInterceptor中:

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

} }

Generally this is not available. 通常这是不可用的。 DynamicProxy intercepts methods (incl. getters and setters), and it does not care about properties. DynamicProxy拦截方法(包括getter和setter),它不关心属性。

You could optimize this code a bit by making the interceptor IOnBehalfAware (see here ) and discovering the method->property mapping upfront. 您可以通过制作拦截器IOnBehalfAware (请参阅此处 )并预先发现method->属性映射来优化此代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM