1

假设我有以下代码:

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 类的实例。有没有办法做到这一点?

谢谢

4

0 回答 0