StructureMap 4.6中的TypeInterceptor实现

时间:2018-01-22 08:21:07

标签: structuremap

有人可以帮我将以下代码行从StructureMap 2.6转换为Structure Map 4.6

public class EventAggregatorInterceptor : TypeInterceptor
{
    public object Process(object target, IContext context)
    {
        IEventPublisher eventPublisher = context.GetInstance<IEventPublisher>();
        eventPublisher.RegisterHandlers(target);
        return target;
    }
    public bool MatchesType(Type type)
    {
        bool matchesType = type.ImplementsInterfaceTemplate(typeof(IEventHandler<>));
        return matchesType;
    }
}

我将不胜感激任何帮助

1 个答案:

答案 0 :(得分:0)

我得到了它,下面是代码

public class EventListenerRegistration : IInterceptorPolicy
{
    public string Description
    {
        get
        {
            return "Adds the constructed object to the EventAggregator";
        }
    }

    public IEnumerable<IInterceptor> DetermineInterceptors(Type pluginType, Instance instance)
    {
        if (instance.ReturnedType.FindInterfacesThatClose(typeof(IEventHandler<>)).Any())
        {
            Expression<Action<IContext, object>> register = (c, o) => c.GetInstance<IEventPublisher>().RegisterHandlers(o);
            yield return new ActivatorInterceptor<object>(register);
        }
    }
}

然后我在我的注册表构造函数

中注册它
Policies.Interceptors(new EventListenerRegistration());