【发布时间】:2018-02-05 14:38:26
【问题描述】:
我有以下拦截器。虽然它做了我打算为我当前的用例做的事情。我发现使用的方法有点 hacky,想知道是否没有更好的方法来做到这一点。
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();
}
}
我只需要拦截指定属性的更改。
待改进: - 这个拦截器拦截所有方法和属性调用,只在特定情况下做一些事情。听起来有点矫枉过正。 - 我需要比较两个相似的字符串...这听起来不是正确的做法。
【问题讨论】:
标签: c# castle castle-dynamicproxy