简体   繁体   English

在温莎城堡设施内创建 class 代理

[英]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.我正在尝试创建一个设施,该设施将根据 class 属性向已注册的 class 添加一些拦截器。 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.但是这样,当我在 class 方法中使用this关键字时,它指的是目标 class 而不是代理 class ,这使得我使用的某些框架无法正常工作。

I need to create with a facility the same proxy that is generated with the ProxyGenerator.CreateClassProxy<MyClass>() method.我需要使用ProxyGenerator.CreateClassProxy<MyClass>()方法生成相同的代理。

How can I achieve this?我怎样才能做到这一点?

Expose the class as a service on your component.将 class 公开为组件上的服务。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM