PostSharp:OnMethodBoundaryAspect 不会被调用
我正在使用PostSharp 将CompoundAspect 应用于ActiveRecord 类(来自CastleProject)。代码如下所示:
public override void ProvideAspects(object targetElement, LaosReflectionAspectCollection collection)
{
Type targetType = (Type)targetElement;
RevertibleSubAspect revertible = new RevertibleSubAspect();
revertible.Cascade = this.Cascade;
collection.AddAspect(targetType, revertible);
//This isn't working
MethodInfo saveMethod = targetType.GetMethod("Save");
collection.AddAspect(saveMethod, new CommitOnSaveSubAspect());
foreach (PropertyInfo property in targetType.GetProperties())
{
if((this.Only != null && this.Only.IndexOf(property.Name) == -1) ||
(this.Except != null && this.Except.IndexOf(property.Name) > -1))
{
continue;
}
if (property.DeclaringType == targetType && property.CanWrite)
{
MethodInfo method = property.GetSetMethod();
if (method != null && !method.IsStatic)
{
collection.AddAspect(method, new TrackInitialPropertyValuesSubAspect());
}
}
}
}
一切正常,除了 CommitOnSaveSubAspect 之外,它是 OnMethodBoundaryAspect。调用 Save 方法时,永远不会调用 OnSuccess 方法。我已经尝试将代码移至 OnEntry 和 OnExit 但这里情况相同。
CommitOnSaveSubAspect 类如下所示:
[Serializable]
class CommitOnSaveSubAspect : OnMethodBoundaryAspect
{
public override void OnSuccess(MethodExecutionEventArgs eventArgs)
{
((IRevertible)eventArgs.Instance).Commit();
}
}
我应用方面的方式是否错误?
I'm using PostSharp to apply a CompoundAspect to a ActiveRecord class (from CastleProject). The code looks like this:
public override void ProvideAspects(object targetElement, LaosReflectionAspectCollection collection)
{
Type targetType = (Type)targetElement;
RevertibleSubAspect revertible = new RevertibleSubAspect();
revertible.Cascade = this.Cascade;
collection.AddAspect(targetType, revertible);
//This isn't working
MethodInfo saveMethod = targetType.GetMethod("Save");
collection.AddAspect(saveMethod, new CommitOnSaveSubAspect());
foreach (PropertyInfo property in targetType.GetProperties())
{
if((this.Only != null && this.Only.IndexOf(property.Name) == -1) ||
(this.Except != null && this.Except.IndexOf(property.Name) > -1))
{
continue;
}
if (property.DeclaringType == targetType && property.CanWrite)
{
MethodInfo method = property.GetSetMethod();
if (method != null && !method.IsStatic)
{
collection.AddAspect(method, new TrackInitialPropertyValuesSubAspect());
}
}
}
}
Everything works fine, except the CommitOnSaveSubAspect which is a OnMethodBoundaryAspect. The OnSuccess method never gets called when the Save method is invoked. I have already tried moving the code to OnEntry and OnExit but same situation here.
The CommitOnSaveSubAspect class looks like this:
[Serializable]
class CommitOnSaveSubAspect : OnMethodBoundaryAspect
{
public override void OnSuccess(MethodExecutionEventArgs eventArgs)
{
((IRevertible)eventArgs.Instance).Commit();
}
}
Am I applying the aspect the wrong way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
调试方面的一个好方法是使用 Reflector 查看生成的程序集。方法是否如您所期望的那样得到增强?
您还可以通过在其中放置断点并使用以下命令行运行 msbuild 来调试 ProvideAspects 方法:
A good way to debug an aspect is to look at the resulting assembly using Reflector. Are methods enhanced as you expect?
You can also debug the ProvideAspects method by putting a breakpoint into it and running msbuild with the following command line:
PostSharp 在安装时是否已全局定义?否则,您必须编辑项目文件才能将 PostSharp 正确注入到程序集中。
请参阅http://doc.postsharp。 org/1.0/Default.aspx##PostSharp.HxS/UserGuide/Platform/EnablingPostSharp.html。
Was PostSharp defined globally when you installed it? Otherwise, you'll have to edit your project files in order for PostSharp to be injected properly into your assemblies.
See http://doc.postsharp.org/1.0/Default.aspx##PostSharp.HxS/UserGuide/Platform/EnablingPostSharp.html.