0

Below is my parent class

public class Parent
{
  //This method is intercept-able using **VirtualMethodInterceptor**
  public virtual void Test()
  {
    //Do something
  }
 }

Below is my child class

public class Child:Parent
{
 // This method directly not intercept-able but it calls base.Test() where   Test    is an intercept-able method
 public void Demo(){
   base.Test();
 }
}

Now I want to resolve an instance of Child class using Unity where Demo method will be interceptable. Actually Demo method can't be interceptable as because it's not virtual but this method internally invoke base.Test() where Test is intercept-able. So how to resolve an interceptable instance of Child class?

It doesn't work If I register child class into an unity container like below

    container.RegisterType<Child>(
            new Interceptor<VirtualMethodInterceptor>(),
            new InterceptionBehavior<Interceptor>()
            )
BartoszKP
  • 34,786
  • 15
  • 102
  • 130
shou
  • 143
  • 1
  • 1
  • 6

1 Answers1

0

Make sure you have done the following:

1) You added the Interception extension like this:

container.AddNewExtension<Interception>();

2) The WillExecute property of your interception behavior class returns true.

3) You obtain the Child instance from the container. This can be done directly like this:

Child chlid = container.Resolve<Child>();

Or by having Child as a dependency for some class, and then resolving the object graph that contains this class using the container.

Yacoub Massad
  • 27,509
  • 2
  • 36
  • 62
  • Though all above are already there in my codes but anyways thanks for your suggestion.. First I want to Resolve child instance which is fine as above. Now, if I call Demo() method using child instance then Test() method should be intercepted as because Demo() internally invoke Test() and Test() is a virtual method so it should be intercepted by VirtualMethodInterceptor behavior – shou Nov 03 '15 at 03:45
  • I tested your scenario and it works in my machine. Can you share a more complete code? – Yacoub Massad Nov 03 '15 at 11:20