コード例 #1
0
        public void Load(ISpeedDateStartable startable, IConfigProvider configProvider,
                         Action <SpeedDateConfig> startedCallback)
        {
            _logger = LogManager.GetLogger("SpeedDate");

            _config = configProvider.Result;

            _container = CreateContainer(startable);

            configProvider.Configure(_container.ResolveAll <IConfig>());

            _container.BuildUp(startable);

            //Filter plugins for namespace & inject configuration into valid plugins
            foreach (var plugin in _container.ResolveAll <IPlugin>())
            {
                if (_config.Plugins.Namespaces.Split(';').Any(ns =>
                                                              Regex.IsMatch(plugin.GetType().Namespace, ns.Trim().AsRegular())))
                {
                    _logger.Debug($"Loading plugin: {plugin}");
                    //Inject configs, cannot use _container.BuildUp because the configProvider may have additional IConfigs
                    var fields = from field in plugin.GetType()
                                 .GetFields(BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public)
                                 where Attribute.IsDefined(field, typeof(InjectAttribute))
                                 select field;

                    foreach (var field in fields)
                    {
                        if (field.GetValue(plugin) == null &&
                            _config.TryGetConfig(field.FieldType.FullName, out var config))
                        {
                            field.SetValue(plugin, config);
                        }
                    }
                }
                else
                {
                    _container.Unregister(typeof(IPlugin), plugin.GetType().FullName);
                }
            }

            //Inject additional dependencies e.g. ILogger
            foreach (var plugin in _container.ResolveAll <IPlugin>())
            {
                _container.BuildUp(plugin);
            }

            //Finally notify every plugin that loading has finished
            foreach (var plugin in _container.ResolveAll <IPlugin>())
            {
                plugin.Loaded();
                _logger.Info($"Loaded {plugin.GetType().Name}");
            }

            startedCallback.Invoke(_config);
        }