我可以在另一个方面运行之前动态生成一个方面吗?

Can I dynamically generate an aspect before another aspect is run?

我会尽量用尽可能少的细节来表达这个问题,以保持问题的简短。让我知道是否需要更多详细信息。

我在 属性 A 上有一个方面 X,它在 属性 B 上动态生成一个方面 Y。属性 B 可能已经包含类型 Y 的方面,并且这些方面与彼此和 Y 的生成实例。

我要求 X 生成的 Y 实例出现在非生成的 Y 实例 运行 之前。使用 AspectDependencies 我无法完成这项工作。我放置了一个 AspectDependencyPosition。

我放在 X 上的方面依赖的形式是: [AspectTypeDependency(AspectDependencyAction.Order, AspectDependencyPosition.Before, typeof(Y))]

我得到的执行顺序是: SourceInstanceOfY,X,GeneratedInstanceOfY

而我需要的执行顺序是: X,SourceInstanceOfY,GeneratedInstanceOfY 最后两个可能会改变顺序。

有没有办法解决这个问题,或者 PostSharp 不支持?

谢谢, 雷米.

通过使用 AspectTypeDependency you specified the ordering between aspects X and Y, but the order for Y instances is still not specified. You can use the ApectPriority 属性 对 Y 方面的各个实例进行排序。 您可以根据下面的描述找到简单的示例:

class TestClass
{
    [MyAspectX]
    public int PropertyA { get; set; }

    [MyAspectY("from attribute", AspectPriority = 2)]
    public int PropertyB { get; set; }
}

[Serializable]
public class MyAspectX : LocationInterceptionAspect, IAspectProvider
{
    public override void OnGetValue(LocationInterceptionArgs args)
    {
        Console.WriteLine("X OnGetValue");
        base.OnGetValue(args);
    }

    public IEnumerable<AspectInstance> ProvideAspects(object targetElement)
    {
        yield return new AspectInstance(
            ((LocationInfo) targetElement).DeclaringType.GetProperty("PropertyB"),
            new MyAspectY("from provider") {AspectPriority = 1});
    }
}

[Serializable]
public class MyAspectY : LocationInterceptionAspect
{
    private string tag;

    public MyAspectY(string tag)
    {
        this.tag = tag;
    }

    public override void OnGetValue(LocationInterceptionArgs args)
    {
        Console.WriteLine("Y OnGetValue " + tag);
        base.OnGetValue(args);
    }
}

class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(new TestClass().PropertyB);
    }
}