在一个项目中,我使用Castle动态代理将try/catch块中的所有由façade运行的代码包装起来(听起来很奇怪吗?解释here)。这工作正常,但要确保所有的方法调用被拦截,我抛出一个异常,当我遇到一些非虚,使用IProxyGenerationHook
接口的NonProxyableMemberNotification
方法:城堡动态代理想拦截Object.GetType()
public void NonProxyableMemberNotification(Type type, MemberInfo memberInfo)
{
throw new InvalidOperationException(string.Format(
"Proxy failure. {0} {1} in {2} is not virtual.",
memberInfo.MemberType, memberInfo.Name, memberInfo.DeclaringType));
}
据克日什托夫·Koźmic的伟大tutorial; 对象类是特殊情况,DynamicProxy默认会忽略它们。问题是,在我的情况下,他们不会被忽略,从下面的示例MemberInfo
数据看出:
有什么我已经在这里错过了吗?是NonProxyableMemberNotification
应该在Object方法上触发?
我使用的是.Net 3.5,VS2010和Castle Core 2.5.2版本,并且我不是在覆盖Object.GetType()
我的XmlDocumentBackend
。
感谢您的答案,这真的解释了很多! – joneberg