How do I specify a particular policy to be used with an interceptable type

Jul 20, 2011 at 3:31 PM
Edited Jul 20, 2011 at 3:36 PM

Suppose I have the following code:

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


			new Interceptor<VirtualMethodInterceptor>(),
			new InterceptionBehavior<PolicyInjectionBehavior>());

					.AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeAttribute), true))
					.AddMatchingRule(new PropertyMatchingRule("*", PropertyMatchingOption.Set))
					.AddMatchingRule(new CustomAttributeMatchingRule(typeof(SomeOTHERAttribute), true))
					.AddMatchingRule(new PropertyMatchingRule("*M", PropertyMatchingOption.Set))

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

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


	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?


Jul 20, 2011 at 8:08 PM

Add a type matching rule to the policies in question to either match or not match the types you want to exclude.


Jul 21, 2011 at 6:45 AM
ctavares wrote:

Add a type matching rule to the policies in question to either match or not match the types you want to exclude.


But those matching rules will still get tried for all kinds of objects that are configured for interception. And this is that additional overhead (type matching rule will have to check the type of objects being resolved) I wand to get rid of.. 

If I could specify a particular policy name when configuring the interception for a type, that would do the trick. Here is how it could look like:

			new Interceptor<VirtualMethodInterceptor>(),
			new InterceptionBehavior<PolicyInjectionBehavior>("ANOTHERPolicyName"));

I.e. when configuring the interceptor for Interceptable I simply tell the container that I want only policy named "ANTOHERPolicyName" to be applied to instances of Interceptable (all other policies get skipped).

Is there any way of doing so?


Jul 22, 2011 at 2:59 PM


I'm afraid Unity doesn't have this functionality. You can log it as a request in our Feature and Issue Tracker.


Noel Angelo Bolasoc
Global Technologies and Solutions
Avanade, Inc.
Contact Us