【发布时间】:2011-10-03 18:33:52
【问题描述】:
在一个项目中,我使用 Castle Dynamic Proxy 将所有由外观运行的代码包装在 try/catch 块中(听起来很奇怪?解释 here)。这很好用,但是为了确保所有方法调用都被拦截,当我遇到非虚拟的东西时,我会抛出一个异常,使用IProxyGenerationHook 接口的NonProxyableMemberNotification 方法:
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));
}
根据 Krzysztof Koźmic 的伟大 tutorial; 对象类是特殊情况,默认情况下 DynamicProxy 会忽略它们。问题是,就我而言,它们没有被忽略,从以下示例 MemberInfo 数据中可以看出:
这里有什么我错过的吗? NonProxyableMemberNotification 是否应该在 Object 方法上触发?
我正在使用 .Net 3.5、VS2010 和 Castle Core 2.5.2 版,并且我没有在我的 XmlDocumentBackend 中覆盖 Object.GetType()。
【问题讨论】:
标签: c# .net castle-dynamicproxy proxy-classes