public void ShouldCreateProxyWithVirtualSetterInitializedInCtor() { var factory = container.GetService <IProxyFactory>(); // Assign the ref/out value for the int argument Func <IInvocationInfo, object> implementation = info => { string methodName = info.TargetMethod.Name; if (methodName == "DoSomething") { info.Arguments[0] = 54321; } if (methodName == "get_SomeProp") { return("blah"); } return(null); }; var interceptor = new MockInterceptor(implementation); var proxy = factory.CreateProxy <SampleClassWithPropertyInitializedInCtor>(interceptor); int value; proxy.DoSomething(out value); // The two given arguments should match Assert.AreEqual("blah", proxy.SomeProp); Assert.AreEqual(54321, value); }
private T CreateProxy <T>(Func <IInvocationInfo, object> implementation) { var factory = container.GetService <IProxyFactory>(); var interceptor = new MockInterceptor(implementation); return(factory.CreateProxy <T>(interceptor)); }
public void ShouldCallInterceptorInstance() { var factory = container.GetService <IProxyFactory>(); var mockInterceptor = new MockInterceptor(i => null); // Create the proxy instance and then make the call var proxyInstance = (ITest)factory.CreateProxy(typeof(object), mockInterceptor, typeof(ITest)); proxyInstance.Execute(); // The interceptor must be called Assert.IsTrue(mockInterceptor.Called); }
public void ShouldAllowProxyToInheritFromMultipleInstancesOfTheSameGenericInterfaceType() { IInterceptor interceptor = new MockInterceptor(body => null); var interfaces = new[] { typeof(IList <int>), typeof(IList <double>), typeof(IList <object>) }; var factory = container.GetService <IProxyFactory>(); var proxy = factory.CreateProxy <object>(interceptor, interfaces); Type proxyType = proxy.GetType(); // The proxy must implement all of the given interfaces foreach (Type currentType in interfaces) { Assert.IsTrue(currentType.IsAssignableFrom(proxyType)); } }
public void ShouldImplementGivenInterfaces() { var interfaces = new[] { typeof(ISampleService), typeof(ISampleGenericService <int>) }; // Note: The interceptor will never be executed var interceptor = new MockInterceptor(info => { throw new NotImplementedException(); }); var factory = container.GetService <IProxyFactory>(); object proxy = factory.CreateProxy(typeof(object), interceptor, interfaces.ToArray()); Type proxyType = proxy.GetType(); // Make sure that the generated proxy implements // all of the given interfaces foreach (Type currentType in interfaces) { Assert.IsTrue(currentType.IsAssignableFrom(proxyType)); } }
public void ShouldSupportSubclassingFromGenericTypes() { var factory = container.GetService <IProxyFactory>(); var actualList = new List <int>(); Func <IInvocationInfo, object> implementation = info => { IList <int> list = actualList; return(info.Proceed(list)); }; var interceptor = new MockInterceptor(implementation); var proxy = factory.CreateProxy <IList <int> >(interceptor); // Any item added to the proxy list should be added to the // actual list proxy.Add(12345); Assert.IsTrue(interceptor.Called); Assert.IsTrue(actualList.Count > 0); Assert.IsTrue(actualList[0] == 12345); }
public void ShouldSupportRefArguments() { var factory = container.GetService <IProxyFactory>(); // Assign the ref/out value for the int argument Func <IInvocationInfo, object> implementation = info => { info.Arguments[0] = 54321; return(null); }; var interceptor = new MockInterceptor(implementation); var proxy = factory.CreateProxy <ClassWithVirtualByRefMethod>(interceptor); int value = 0; proxy.ByRefMethod(ref value); // The two given arguments should match Assert.AreEqual(54321, value); }