Guest User

Untitled

a guest
Mar 14th, 2018
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using System;
  2. using System.Diagnostics;
  3. using Castle.Core;
  4. using Castle.Core.Interceptor;
  5. using Castle.MicroKernel.Handlers;
  6. using Castle.MicroKernel.Proxy;
  7. using Castle.MicroKernel.Registration;
  8. using Castle.Windsor;
  9.  
  10. namespace ConsoleApplication3
  11. {
  12. class Program
  13. {
  14. static void Main(string[] args)
  15. {
  16. var container = new WindsorContainer();
  17. container.Kernel.HandlerRegistered += BugWorkaround;
  18. container.Register( Component.For<IInterceptor>().Named( "interceptor" ).ImplementedBy<FooBarInterceptor>(),
  19. Component.For<Bar>().Forward<IFoo>().ImplementedBy<FooBar>()
  20. .Interceptors( new InterceptorReference( "interceptor" ) ).Anywhere );
  21. var foo = container.Resolve<IFoo>();
  22. var bar = container.Resolve<Bar>();
  23. Debug.Assert( foo == bar );
  24. foo.DoFoo();
  25. bar.DoBar();
  26. Console.ReadKey( true );
  27. }
  28.  
  29. static void BugWorkaround(Castle.MicroKernel.IHandler handler, ref bool stateChanged)
  30. {
  31. var forwardingHandler = handler as ForwardingHandler;
  32. if (forwardingHandler == null)
  33. return;
  34. var targetHandler = forwardingHandler.Target;
  35. var options = targetHandler.ComponentModel.ExtendedProperties[ ProxyConstants.ProxyOptionsKey ] as ProxyOptions;
  36. if(options == null)
  37. return;
  38.  
  39. options.AddAdditionalInterfaces( forwardingHandler.Service );
  40.  
  41. }
  42. }
  43.  
  44. public class FooBarInterceptor : IInterceptor
  45. {
  46. public void Intercept(IInvocation invocation)
  47. {
  48. Console.WriteLine( "Intercepted " + invocation.Method.Name );
  49. invocation.Proceed();
  50. }
  51. }
  52.  
  53. public class FooBar:Bar,IFoo {
  54. public void DoFoo()
  55. {
  56. Console.WriteLine( "DoFoo" );
  57. }
  58.  
  59. public override void DoBar()
  60. {
  61. Console.WriteLine("DoBar");
  62. }
  63. }
  64.  
  65. public abstract class Bar
  66. {
  67. public abstract void DoBar();
  68. }
  69.  
  70. public interface IFoo
  71. {
  72. void DoFoo();
  73. }
  74. }
Add Comment
Please, Sign In to add comment