Castle Windsor:使用拦截器创建组件事件

时间:2012-01-19 09:48:12

标签: castle-windsor interceptor castle windsor-facilities

我已经为城堡windsor创建了一个自动配置实例的工具,除了带有拦截器的类型外,它工作得很好!在那种情况下,ComponentCreated事件不会触发,我无法配置我的实例,我如何拦截代理类的创建? 这是我的设施代码:

_conf = this.Kernel.Resolve<Configuration>();

        this.Kernel.ComponentCreated += (s, o) =>
        {
            //Subscribe the every IHandler to the caliburn micro event aggregator
            if (typeof(IHandle).IsAssignableFrom(o.GetType()))
                Kernel.Resolve<IEventAggregator>().Subscribe(o);

            var attr = o.GetType().GetCustomAttributes(false).OfType<ConfigurableAttribute>().FirstOrDefault();
            SettingsCollection setter = null;
            if (attr != default(ConfigurableAttribute) && (setter = _conf.GetSettings(attr.Name)) != null)
                setter.Set(o);
        };

解决方案

ComponentCreated事件参数“s”是一个ComponentModel,因此它在Implementation属性中保存了unproxied类型! 我的固定代码如下所示:

this.Kernel.ComponentCreated += (s, o) =>
  {
    var attr = o.GetType().GetCustomAttributes(false).OfType<ConfigurableAttribute>().FirstOrDefault();
    SettingsCollection setter = null;
    if(attr != default(ConfigurableAttribute) && (setter = _conf.GetSettings(attr.Name)) != null)
      setter.Set(o);
  }

0 个答案:

没有答案