我正在使用castle windsor interceptor尝试启动和完成所有公共方法的事务。
我有这个Intercept
方法:
public void Intercept(IInvocation invocation)
{
MethodInfo method;
try
{
method = invocation.MethodInvocationTarget;
}
catch
{
method = invocation.GetConcreteMethod();
}
if (!method.IsPublic)
{
return;
}
if (!((IList) new[] {"ncontinuity2.core", "c2.bases"}).Contains(method.DeclaringType.Assembly.GetName().Name))
{
return;
}
PerformUow(invocation);
}
我找不到排除属性设置方法的方法,例如,我在基类中拥有此属性:
public virtual Context Context
{
get { return _context; }
set
{
_context = value;
}
}
我想排除类似Set_Context
的属性。
我怎么知道它是一个属性,有没有办法知道它是否在基类中?
要判断方法是否继承,可以将DeclaringType
与实际对象类型进行比较。我不确定Castle-Windsor部分,但应该是这样的
invocation.TargetType == method.DeclaringType
对于属性访问者,IsSpecialName
属性等于true
。
!method.IsSpecialName
together
if (invocation.TargetType == method.DeclaringType && !method.IsSpecialName) {
// We have a non-inherited method not being a property accessor.
}