我正在使用 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();
}
}
我是否以错误的方式应用方面?