public IGenerationModel ToGenerationModel(IGenerationConfig config) { if (!Handlers.Any()) { throw new InvalidOperationException("No method handlers configured for message type " + MessageType.FullName); } var configureMethods = Handlers.Select(x => x.HandlerType).Distinct() .Select(x => x.GetTypeInfo().GetMethod("Configure", new Type[] { typeof(HandlerChain) })); foreach (var method in configureMethods) { method?.Invoke(null, new object[] { this }); } foreach (var methodCall in Handlers.ToArray()) { methodCall.Method.ForAttribute <ModifyHandlerChainAttribute>(att => att.Modify(this)); } foreach (var handlerType in Handlers.Select(x => x.HandlerType).Distinct().ToArray()) { handlerType.ForAttribute <ModifyHandlerChainAttribute>(att => att.Modify(this)); } var i = 0; var cascadingHandlers = Handlers.Where(x => x.ReturnVariable != null) .Select(x => new CaptureCascadingMessages(x.ReturnVariable, ++i)); var frames = Wrappers.Concat(Handlers).Concat(cascadingHandlers).ToList(); return(new MessageHandlerGenerationModel(TypeName, MessageType, config, frames)); }