コード例 #1
0
ファイル: PluginManager.cs プロジェクト: lvyitian/Alex
        public void ConfigureServices(IServiceCollection serviceCollection)
        {
            Dictionary <Assembly, List <Plugin> > assemblies = new Dictionary <Assembly, List <Plugin> >();

            foreach (var grouped in PluginConstructors.Where(x => typeof(IServiceHolder).IsAssignableFrom(x.Type)).GroupBy(x => x.Type.Assembly).ToArray())
            {
                List <Plugin> assemblyInstances = new List <Plugin>();
                foreach (var constructor in grouped)
                {
                    if (constructor.RequiresServices)
                    {
                        Log.Warn($"Could not configure plugin as ServiceHolder. IServiceHolder implementations require an empty Constructor! (Type: {constructor.Type})");
                        continue;
                    }

                    if (CreateInstance(constructor, out Plugin instance, assemblies))
                    {
                        PluginContainer.RegisterSingleton(instance.GetType(), instance);

                        IServiceHolder serviceHolder = instance as IServiceHolder;
                        serviceHolder?.ConfigureServices(serviceCollection);
                        // pluginInstances++;

                        assemblyInstances.Add(instance);
                        //serviceCollection.AddSingleton(constructor.Type, instance);
                    }

                    PluginConstructors.Remove(constructor);
                }

                if (!assemblies.ContainsKey(grouped.Key))
                {
                    assemblies.Add(grouped.Key, assemblyInstances);
                }
                else
                {
                    assemblies[grouped.Key].AddRange(assemblyInstances);
                }

                //PluginConstructors.Remove(constructor);
            }

            foreach (var grouped in assemblies)
            {
                if (LoadedAssemblies.ContainsKey(grouped.Key))
                {
                    LoadedAssemblies[grouped.Key].PluginTypes.AddRange(grouped.Value.Select(x => x.GetType()));
                    continue;
                }
                else
                {
                    LoadedAssemblies.Add(grouped.Key,
                                         new LoadedAssembly(grouped.Key, grouped.Value.Select(x => x.GetType()), new Assembly[0],
                                                            grouped.Key.Location));
                }
            }
        }
コード例 #2
0
 public void RegisterHolder(IServiceHolder holder)
 {
     ServiceHolders.Add(holder);
 }