指定可拦截类型的策略

发布于 2024-11-25 10:28:12 字数 1542 浏览 6 评论 0原文

假设我有以下代码:

class Program
{
static void Main(string[] args)
{
    IUnityContainer container = new UnityContainer();

    container.AddNewExtension<Interception>();

    container.RegisterType<Interceptable>(
        new Interceptor<VirtualMethodInterceptor>(),
        new InterceptionBehavior<PolicyInjectionBehavior>());

    container
        .Configure<Interception>()
            .AddPolicy("PolicyName")
                .AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeAttribute), true))
                .AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.Set))
                .AddCallHandler<PropertySetterCallHandler>();
        .Interception
            .AddPolicy("AnotherPolicyName")
                .AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeOTHERAttribute), true))
                .AddMatchingRule(new PropertyMatchingRule("*M", PropertyMatchingOption.Set))
                .AddCallHandler<ANOTHERPropertySetterCallHandler>();


        var ic = container.Resolve<Interceptable>();

        //property setter is invoked - the matching rules from BOTH the policies will be tried
        ic.Property = 2;

        Console.ReadLine();
}

class Interceptable
{
    public virtual int Property  {  get; [SomeAttribute]set; }
}

}

按照现在的配置方式,每次我解析 Interceptable 实例并在其上调用公共虚拟方法时,都会尝试 4 个匹配规则(来自两个策略)。

这可能会在现实应用程序中产生开销。我想要做的是指定我只想(例如)“PolicyName”策略应用于 Interceptable 类的实例。有什么办法可以做到这一点吗?

谢谢

Suppose I have the following code:

class Program
{
static void Main(string[] args)
{
    IUnityContainer container = new UnityContainer();

    container.AddNewExtension<Interception>();

    container.RegisterType<Interceptable>(
        new Interceptor<VirtualMethodInterceptor>(),
        new InterceptionBehavior<PolicyInjectionBehavior>());

    container
        .Configure<Interception>()
            .AddPolicy("PolicyName")
                .AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeAttribute), true))
                .AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.Set))
                .AddCallHandler<PropertySetterCallHandler>();
        .Interception
            .AddPolicy("AnotherPolicyName")
                .AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeOTHERAttribute), true))
                .AddMatchingRule(new PropertyMatchingRule("*M", PropertyMatchingOption.Set))
                .AddCallHandler<ANOTHERPropertySetterCallHandler>();


        var ic = container.Resolve<Interceptable>();

        //property setter is invoked - the matching rules from BOTH the policies will be tried
        ic.Property = 2;

        Console.ReadLine();
}

class Interceptable
{
    public virtual int Property  {  get; [SomeAttribute]set; }
}

}

The way it's configured now, every time I resolve an instance of Interceptable and call a public virtual method on it, 4 matching rules get tried (from both the policies).

This may create an overhead in a real-world app. What I'd like to do is to specify that I want only (for example) 'PolicyName' policy to be applied to instances of Interceptable class. Is there any way to do this?

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文