public void InterceptorCreatesProxyInstance() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); ImplementsInterfaceOne target = new ImplementsInterfaceOne(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IInterfaceOne), target); IInterfaceOne intercepted = (IInterfaceOne)proxy; intercepted.TargetMethod(); Assert.IsTrue(target.TargetMethodCalled); }
public void GeneratedProxyCallsInterceptionBehaviors() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); ImplementsInterfaceOne target = new ImplementsInterfaceOne(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IInterfaceOne), target); CallCountInterceptionBehavior interceptionBehavior = new CallCountInterceptionBehavior(); proxy.AddInterceptionBehavior(interceptionBehavior); IInterfaceOne intercepted = (IInterfaceOne)proxy; intercepted.TargetMethod(); intercepted.TargetMethod(); intercepted.TargetMethod(); Assert.AreEqual(3, interceptionBehavior.CallCount); }
public void GeneratedProxyCallsHandlers() { IInstanceInterceptor interceptor = new InterfaceInterceptor(); ImplementsInterfaceOne target = new ImplementsInterfaceOne(); IInterceptingProxy proxy = interceptor.CreateProxy(typeof(IInterfaceOne), target); CallCountHandler handler = new CallCountHandler(); proxy.SetPipeline(typeof(IInterfaceOne).GetMethod("TargetMethod"), new HandlerPipeline(new ICallHandler[] { handler })); IInterfaceOne intercepted = (IInterfaceOne)proxy; intercepted.TargetMethod(); intercepted.TargetMethod(); intercepted.TargetMethod(); Assert.AreEqual(3, handler.CallCount); }
public void InterfaceInterceptorSetsTargetToTargetObject() { object suppliedTarget = null; var behavior = new DelegateInterceptionBehavior((inputs, getNext) => { suppliedTarget = inputs.Target; return(getNext()(inputs, getNext)); }); var actualTarget = new ImplementsInterfaceOne(); var proxy = Intercept.ThroughProxy <IInterfaceOne>( actualTarget, new InterfaceInterceptor(), new[] { behavior }); proxy.TargetMethod(); Assert.IsTrue(actualTarget.TargetMethodCalled); Assert.AreSame(actualTarget, suppliedTarget); }