2017-08-28 100 views
0

我有以下拦截器。虽然它按照我打算为我目前的使用情况做的那样做。我发现使用的方法有点冒险,并想知道是否没有更好的方法来做到这一点。在Castle Dynamic Proxy拦截器中检查是否为特定属性调用了

public class Interceptor<TEntity, TProperty> : IInterceptor 
{ 
    private readonly Expression<Func<TEntity, TProperty>> _propertySelector; 

    public Interceptor(Expression<Func<TEntity, TProperty>> propertySelector) 
    { 
     _propertySelector = propertySelector; 
    } 

    public void Intercept(IInvocation invocation) 
    { 
     var invocatedMethod = invocation.Method.Name; 
     var selectedMethod = (_propertySelector.Body as MemberExpression)?.Member.Name; 
     if (invocatedMethod == $"set_{selectedMethod}") 
     { 
      //do stuff... 
     } 
     invocation.Proceed(); 
    } 
} 

我需要的是拦截指定属性的更改。

待改进: - 此拦截器拦截所有方法和属性调用,仅在特定情况下执行某些操作。这听起来像是矫枉过正。 - 我需要比较两个类似的字符串......这听起来像是不正确的做法。

回答