Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/31.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# PostSharp:OnMethodBoundaryAspect不';我没接到电话_C#_Aop_Castle Activerecord_Postsharp - Fatal编程技术网

C# PostSharp:OnMethodBoundaryAspect不';我没接到电话

C# PostSharp:OnMethodBoundaryAspect不';我没接到电话,c#,aop,castle-activerecord,postsharp,C#,Aop,Castle Activerecord,Postsharp,我使用PostSharp将CompoundSpect应用于ActiveRecord类(来自CastleProject)。代码如下所示: public override void ProvideAspects(object targetElement, LaosReflectionAspectCollection collection) { Type targetType = (Type)targetElement; RevertibleSubAspect revertible =

我使用PostSharp将CompoundSpect应用于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());
            }
        }
    }
}
[Serializable]
class CommitOnSaveSubAspect : OnMethodBoundaryAspect
{
    public override void OnSuccess(MethodExecutionEventArgs eventArgs)
    {
        ((IRevertible)eventArgs.Instance).Commit();
    }
}
除了CommitonSaveSubspect(一个OnMethodBoundaryAspect)之外,一切都正常。调用Save方法时,永远不会调用OnSuccess方法。我已经尝试将代码移动到onetry和OnExit,但这里的情况相同

CommittenSaveSubspect类如下所示:

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());
            }
        }
    }
}
[Serializable]
class CommitOnSaveSubAspect : OnMethodBoundaryAspect
{
    public override void OnSuccess(MethodExecutionEventArgs eventArgs)
    {
        ((IRevertible)eventArgs.Instance).Commit();
    }
}

我是否以错误的方式应用了方面?

安装后,PostSharp是否在全局范围内定义了它?否则,您必须编辑项目文件,以便将PostSharp正确地注入到程序集中


请参阅。

调试方面的一个好方法是使用Reflector查看生成的程序集。方法是否如您所期望的那样得到了增强

还可以通过在ProvideSpects方法中放置断点并使用以下命令行运行msbuild来调试该方法:

msbuild /p:PostSharpAttachDebugger=true

PostSharp是在我安装它时全局定义的。如果它不是全局定义的,那么上面的所有代码都不会工作。但只有CommittenSaveSave子方面不起作用。@Mato:只是检查一下!method.IsStatic和其他if条件。您确定方面已分配且未被这些条件语句阻止吗?为什么不使用NHibernate/ActiveRecord的常规拦截机制?(override OnSave()或finddrity(),IInterceptor,事件侦听器)@David Andres:committensavesubspect应用于foreach循环之外,因此条件语句不会影响这一点@Mauricio Scheffer:我不想将代码扩展到许多类上,因为这种方式的可重用性更好。不这样做的另一个原因是,在我看来,这不是propper AOP。好的,看起来该方法没有像预期的那样得到增强(它根本没有被更改)。但是调试器显示getMethod(“Save”)找到该方法并返回正确的MethodInfo。问题是这种方法是虚拟的吗?当我覆盖类中的方法时,我正在将属性应用到方法中,正如预期的那样得到增强。当向虚拟方法添加方面时,该方面将应用到使用该方面注释的精确方法。有关也适用于覆盖的方面的详细信息,请参见。