aop - 您可以在不使用属性的情况下在 PostSharp 中应用方面吗?

标签 aop postsharp

我知道使用 CaSTLe Windsor,您可以使用代码注册方面(在 Windsor 中使用方法拦截作为 AOP 时),而不是将属性应用于类。在 Postsharp 中是否有同样的可能?这是一种偏好,但更喜欢将方面与接口(interface)/对象匹配在一个地方,而不是全部属性。

更新:
好奇我是否可以将方面分配给与此类似的接口(interface)/对象:

container.Register(
        Component
        .For<IService>()
        .ImplementedBy<Service>()
        .Interceptors(InterceptorReference.ForType<LoggingAspect>()).Anywhere
   );

如果你能做到这一点,你可以选择不必在程序集/类/方法上放置属性来应用方面。然后我可以拥有一个代码文件/类,其中包含哪些方面应用于哪些类/方法/等。

最佳答案

是的。您可以使用多播(http://www.sharpcrafters.com/blog/post/Day-2-Applying-Aspects-with-Multicasting-Part-1.aspx,http://www.sharpcrafters.com/blog/post/Day-3-Applying-Aspects-with-Multicasting-Part-2.aspx)或者您可以使用方面提供者(http ://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-12-e28093-Aspect-Providers-e28093-Part-1.aspx,http://www.sharpcrafters.com/blog/post/PostSharp-Principals-Day-13-e28093-Aspect-Providers-e28093-Part-2.aspx)。

例子:

    using System;
    using PostSharp.Aspects;
    using PostSharp.Extensibility;

    [assembly: PostSharpInterfaceTest.MyAspect(AttributeTargetTypes = "PostSharpInterfaceTest.Interface1", AttributeInheritance = MulticastInheritance.Multicast)]

    namespace PostSharpInterfaceTest
{
    class Program
    {
        static void Main(string[] args)
        {
            Example e = new Example();
            Example2 e2 = new Example2();
            e.DoSomething();
            e2.DoSomething();
            Console.ReadKey();
        }
    }

    class Example : Interface1
    {

        public void DoSomething()
        {
            Console.WriteLine("Doing something");
        }
    }

    class Example2 : Interface1
    {

        public void DoSomething()
        {
            Console.WriteLine("Doing something else");
        }
    }

    interface Interface1
    {
        void DoSomething();
    }

    [Serializable]
    class MyAspect : OnMethodBoundaryAspect
    {
        public override void OnEntry(MethodExecutionArgs args)
        {
            Console.WriteLine("Entered " + args.Method.Name);
        }
    }
}

我建议,如果您对确定哪些类型获得某些方面有复杂的要求,您可以考虑创建一个方面提供程序。

关于aop - 您可以在不使用属性的情况下在 PostSharp 中应用方面吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7796077/

相关文章:

xamarin.ios - 具有MonoTouch的AOP

java - 通过注释/字节码编织跟踪所有权/祖先层次结构/组成员身份?

java - Spring AOP : Choosing advice seletively

aop - 如何在 spring aop 中将上下文参数传递给建议

c# - 如何根据条件退出 PostSharp 方面的 OnEntry 方法中的方法

c# - 如何测量.NET Core中所有方法的执行时间?

java - 我可以使用 AspectJ/AOP 在 Java 类之间安装超出正常可见性规则的编译时访问规则吗?

.net - teamcity 上的 Postsharp 问题

c# - 如何使用自定义属性为 C# 自动属性提供默认值?