gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 15:26:11 25 4
gpt4 key购买 nike

我知道使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com