【问题标题】:Create a class proxy within a castle windsor facility在温莎城堡设施内创建类代理
【发布时间】:2011-07-17 12:20:39
【问题描述】:

我正在尝试创建一个工具,该工具将根据类属性将一些拦截器添加到已注册的类中。 这是我的设施:

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>());
            }
        }
    }
}

但是这样,当我在类方法中使用 this 关键字时,它指的是目标类而不是代理类,这使得我使用的某些框架无法正常工作。

我需要使用ProxyGenerator.CreateClassProxy&lt;MyClass&gt;() 方法生成的相同代理创建一个工具。

我怎样才能做到这一点?

【问题讨论】:

  • 你能显示更多代码吗?你注册的组件是接口还是类?
  • 我注册的组件是一个接口。这是 Windsor 安装: _wc = new WindsorContainer(); _wc.AddFacility(); _wc.Register( Component.For() ); _wc.Register(Component.For().ImplementedBy());

标签: c# castle-windsor facilities


【解决方案1】:

将类公开为组件上的服务。

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

【讨论】:

  • 如何将类公开为服务?可以发个例子吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多