Search code examples
c#castle-windsorfacilities

Create a class proxy within a castle windsor facility


I'm trying to create a facility that will add some interceptor to the registered class based on the class attribute. This is my facility:

public class MyFacility : AbstractFacility
{
     protected override void Init()
    {
        this.Kernel.ComponentRegistered += (s, h) =>
        {
            if (h.ComponentModel.Implementation.GetCustomAttributes(typeof(MyAttribute), false).Length > 0)
            {
                h.ComponentModel.Interceptors.Add(InterceptorReference.ForType<MyInterceptor>());
            }
        }
    }
}

but this way, when I use the this keyword in a class method it refers to the target class not the proxy class and this makes some framework that I use don't work properly.

I need to create with a facility the same proxy that is generated with the ProxyGenerator.CreateClassProxy<MyClass>() method.

How can I achieve this?


Solution

  • Expose the class as a service on your component.

    container.Register(
       Component.For<SomeClass,ISomeInterface>().Lifestyle.Whatever.Interceptors<SomeInterceptor>()
    );