Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Diagnostics;
- using Castle.Core;
- using Castle.Core.Interceptor;
- using Castle.MicroKernel.Handlers;
- using Castle.MicroKernel.Proxy;
- using Castle.MicroKernel.Registration;
- using Castle.Windsor;
- namespace ConsoleApplication3
- {
- class Program
- {
- static void Main(string[] args)
- {
- var container = new WindsorContainer();
- container.Kernel.HandlerRegistered += BugWorkaround;
- container.Register( Component.For<IInterceptor>().Named( "interceptor" ).ImplementedBy<FooBarInterceptor>(),
- Component.For<Bar>().Forward<IFoo>().ImplementedBy<FooBar>()
- .Interceptors( new InterceptorReference( "interceptor" ) ).Anywhere );
- var foo = container.Resolve<IFoo>();
- var bar = container.Resolve<Bar>();
- Debug.Assert( foo == bar );
- foo.DoFoo();
- bar.DoBar();
- Console.ReadKey( true );
- }
- static void BugWorkaround(Castle.MicroKernel.IHandler handler, ref bool stateChanged)
- {
- var forwardingHandler = handler as ForwardingHandler;
- if (forwardingHandler == null)
- return;
- var targetHandler = forwardingHandler.Target;
- var options = targetHandler.ComponentModel.ExtendedProperties[ ProxyConstants.ProxyOptionsKey ] as ProxyOptions;
- if(options == null)
- return;
- options.AddAdditionalInterfaces( forwardingHandler.Service );
- }
- }
- public class FooBarInterceptor : IInterceptor
- {
- public void Intercept(IInvocation invocation)
- {
- Console.WriteLine( "Intercepted " + invocation.Method.Name );
- invocation.Proceed();
- }
- }
- public class FooBar:Bar,IFoo {
- public void DoFoo()
- {
- Console.WriteLine( "DoFoo" );
- }
- public override void DoBar()
- {
- Console.WriteLine("DoBar");
- }
- }
- public abstract class Bar
- {
- public abstract void DoBar();
- }
- public interface IFoo
- {
- void DoFoo();
- }
- }
Add Comment
Please, Sign In to add comment