有人可以帮我将以下代码行从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;
}
}
我将不胜感激任何帮助
答案 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());