public void MixinForInterfaces() { GeneratorContext context = new GeneratorContext(); SimpleMixin mixin_instance = new SimpleMixin(); context.AddMixinInstance(mixin_instance); AssertInvocationInterceptor interceptor = new AssertInvocationInterceptor(); MyInterfaceImpl target = new MyInterfaceImpl(); object proxy = _generator.CreateCustomProxy( typeof(IMyInterface), interceptor, target, context); Assert.IsNotNull(proxy); Assert.IsTrue(typeof(IMyInterface).IsAssignableFrom(proxy.GetType())); Assert.IsFalse(interceptor.Invoked); ISimpleMixin mixin = proxy as ISimpleMixin; Assert.IsNotNull(mixin); Assert.AreEqual(1, mixin.DoSomething()); Assert.IsTrue(interceptor.Invoked); Assert.AreSame(proxy, interceptor.proxy); Assert.AreSame(mixin_instance, interceptor.mixin); }
/// <summary> /// Creates a proxy for an XML-RPC service. /// </summary> /// <typeparam name="T">XML-RPC service interface.</typeparam> /// <returns></returns> public T CreateProxy <T>() { // // First, create mixin XmlRpcServiceProxy mixin = new XmlRpcServiceProxy(typeof(T)); mixin.WebRequestFactory = new HttpXmlRpcWebRequestFactory(); // // Now create implementation GeneratorContext generatorContext = new GeneratorContext(); generatorContext.AddMixinInstance(mixin); ProxyGenerator proxyGenerator = new ProxyGenerator(); return((T)proxyGenerator.CreateCustomProxy(typeof(T), new XmlRpcServiceProxyInterceptor(), mixin, generatorContext)); }
/// <summary> /// /// </summary> /// <param name="kernel"></param> /// <param name="model"></param> /// <param name="constructorArguments"></param> /// <returns></returns> public override object Create(IKernel kernel, ComponentModel model, params object[] constructorArguments) { IMethodInterceptor[] interceptors = ObtainInterceptors(kernel, model); IInterceptor interceptorChain = new InterceptorChain(interceptors); // This is a hack to avoid unnecessary object creation // and unecessary delegations. // We supply our contracts (Interceptor and Invocation) // and the implementation for Invocation dispatchers // DynamicProxy should be able to use them as long as the // signatures match GeneratorContext context = new GeneratorContext(); context.Interceptor = typeof(IMethodInterceptor); context.Invocation = typeof(IMethodInvocation); context.SameClassInvocation = typeof(DefaultMethodInvocation); context.InterfaceInvocation = typeof(DefaultMethodInvocation); CustomizeContext(context, kernel, model, constructorArguments); object proxy = null; if (model.Service.IsInterface) { Object target = Activator.CreateInstance(model.Implementation, constructorArguments); proxy = generator.CreateCustomProxy(CollectInterfaces(model.Service, model.Implementation), interceptorChain, target, context); } else { proxy = generator.CreateCustomClassProxy(model.Implementation, interceptorChain, context, constructorArguments); } CustomizeProxy(proxy, context, kernel, model); return(proxy); }