VirtualMethodInterception on all classes inheriting from a base class

Jul 14, 2010 at 9:15 AM
Hi, I've got Virtual Method Interception working just fine in standalone mode, but I'm now trying to configure it through Unity. ALL the examples show registering a specific type for interception. How can I do it in a more generic fashion? (e.g. all classes inheriting from a base class, or all classes marked with an Attribute) It seems that Matching rules are only used for ICallHandlers? Thanks, D
Jul 14, 2010 at 10:44 AM

You'll use the DefaultInterceptor and DefaultInterceptionBehavior instead of the Interceptor and InterceptionBehavior classes.  It is included in the documentation. Refer here for the link on the topic.

Sarah Urmeneta
Global Technology and Solutions
Avanade, Inc.

Jul 14, 2010 at 2:54 PM
Edited Jul 14, 2010 at 5:57 PM

I've tried the following:

var container = new UnityContainer(); container.AddNewExtension<Interception>();

new DefaultInterceptor<VirtualMethodInterceptor>(),
new DefaultInterceptionBehavior<NotifyPropertyChangedCallHandler>(),
new AdditionalInterface<INotifyPropertyChanged>());

var child = container.Resolve<ChildClass>();

ChildClass inherits from DummyBaseClass but interception does not happen?

Thanks, D.

Jul 14, 2010 at 7:18 PM
Edited Jul 14, 2010 at 7:25 PM

Registration doesn't look up class hierarchies - you need to register the concrete types explicitly.

Also, just as a note, you probably don't need to explicitly give the new AddtionalInterface<INotifyPropertyChanged>() configuration; your behavior should be supplying that automatically.


Jul 15, 2010 at 9:04 AM
Ok thanks, Then I should ask a different question: What can or should I do to "enable" this functionality? I would prefer not to have to go and register each and every business class explicitly in unity. I was thinking along the lines of writing a custom extension to go an add the necessary policies in pre-build up before the Interception extension kicks in? Thanks, D
Jul 15, 2010 at 10:03 AM

I've coded the following as a POC:


      var container = new UnityContainer();
        new DefaultInterceptor<VirtualMethodInterceptor>(),
        new DefaultInterceptionBehavior<NotifyPropertyChangedCallHandler>());
      var child = container.Resolve<ChildClass>();


  public class DummyBaseClass

  public class ChildClass : DummyBaseClass

Other classes

  public class PoliciesExtension: UnityContainerExtension
    protected override void Initialize()

    public PoliciesExtension SetPolicesFor<T>(params InjectionMember[] injectionMembers)
      where T: Attribute
      Context.Policies.Set<IAttributePolicy>(new AttributePolicy(typeof (T), injectionMembers), typeof(T));
      return this;


  public interface IAttributePolicy: IBuilderPolicy
    Type AttributeType { get; }
    InjectionMember[] InjectionMembers { get; }

  public class PoliciesStrategy : BuilderStrategy
    public override void PreBuildUp(IBuilderContext context)
      var attrs = context.BuildKey.Type.GetCustomAttributes(true);
      foreach(var attr in attrs)
        var policy = context.PersistentPolicies.Get<IAttributePolicy>(attr.GetType(), false);
        if (policy == null)
        foreach(var mbr in policy.InjectionMembers)
          mbr.AddPolicies(null, context.BuildKey.Type, null, context.Policies);
} }

Does this look like it can work?