Unity提供了丰富的拦截匹配策略,同时它也提供了ApplyNoPoliciesAttribute用于对特定的成员不应用拦截策略。看一个简单的示例:
1 public class MyObject 2 { 3 [ApplyNoPolicies] 4 public virtual Int32 DoWork(Int32 i, Char c) 5 { 6 return i; 7 } 8 9 public virtual void DoWork2(Int32 i, Char c) 10 { 11 12 } 13 14 public virtual void DoWork3() 15 { 16 17 } 18 } 19 20 IUnityContainer unityContainer = new UnityContainer(); 21 22 unityContainer.LoadConfiguration(); 23 unityContainer.Configure<Interception>() 24 .AddPolicy(“TypeMatchingRule”) 25 .AddMatchingRule(new TypeMatchingRule(“MyObject”)) 26 .AddCallHandler<Log4NetHandler>(); 27 unityContainer.RegisterType<MyObject>( 28 new Interceptor<VirtualMethodInterceptor>(), 29 new InterceptionBehavior<PolicyInjectionBehavior>() 30 ); 31 32 MyObject myObject = unityContainer.Resolve<MyObject>(); 33 34 myObject.DoWork(Int32.MaxValue, Char.MaxValue); 35 myObject.DoWork2(Int32.MaxValue, Char.MaxValue); 36 myObject.DoWork3();
MyObject类型的DoWork函数不会被拦截。