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