private static IPluginLoader createPluginLoader(IServiceCollection services, PluginFactoryOptions options) { IPluginLoader loader = new DefaultPluginLoader(options, services); loader.Load(); foreach (PluginInfo pi in loader.PluginList) { if (!pi.IsEnable) { continue; } services.TryAddEnumerable(ServiceDescriptor.Singleton(typeof(IPlugin), pi.PluginType)); } // 注入配置映射 var list = loader.PluginList.Where(x => x.CanConfig && x.IsEnable).ToList(); foreach (PluginInfo pi in list) { Type cfgOptionsType = typeof(IConfigureOptions <>).MakeGenericType(pi.ConfigType); Type impleType = typeof(PluginConfigrationOptions <,>).MakeGenericType(pi.PluginType, pi.ConfigType); services.TryAddEnumerable(ServiceDescriptor.Singleton(cfgOptionsType, impleType)); } loader.Init(); return(loader); }
public void Test2() { PluginFactoryOptions options = new PluginFactoryOptions(); IServiceCollection services = new ServiceCollection(); options.PluginPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Plugins"); options.FileProvider = new PhysicalFileProvider(options.PluginPath); IPluginLoader loader = new DefaultPluginLoader(options, services); loader.Load(); Assert.Equal(4, loader.PluginList.Count); Assert.Equal(4, loader.PluginList.Count); var pi = loader.PluginList.First(p => p.Alias == "TestPlugin"); Assert.Equal("TestPlugin", pi.Name); Assert.True(pi.IsEnable); Assert.False(pi.CanInit); Assert.False(pi.CanConfig); pi = loader.PluginList.First(p => p.Alias == "TestInitPlugin"); Assert.Equal("TestInitPlugin", pi.Name); Assert.True(pi.IsEnable); Assert.True(pi.CanInit); Assert.False(pi.CanConfig); pi = loader.PluginList.First(p => p.Alias == "TestConfigPlugin"); Assert.Equal("TestConfigPlugin", pi.Name); Assert.True(pi.IsEnable); Assert.False(pi.CanInit); Assert.True(pi.CanConfig); Assert.Equal("TestPluginA.TestConfigPluginOptions", pi.ConfigType.FullName); pi = loader.PluginList.First(p => p.Alias == "TestConfigPluginWithInit"); Assert.Equal("TestConfigPluginWithInit", pi.Name); Assert.True(pi.IsEnable); Assert.True(pi.CanInit); Assert.True(pi.CanConfig); Assert.Equal("TestPluginA.TestConfigPluginWithInitOptions", pi.ConfigType.FullName); }
public void Test1() { PluginFactoryOptions options = new PluginFactoryOptions(); IServiceCollection services = new ServiceCollection(); options.PluginPath = AppDomain.CurrentDomain.BaseDirectory; options.FileProvider = new PhysicalFileProvider(options.PluginPath); options.AddAssembly(Assembly.GetExecutingAssembly()); IPluginLoader loader = new DefaultPluginLoader(options, services); loader.Load(); Assert.Equal(4, loader.PluginList.Count); var pi = loader.PluginList.First(p => p.PluginType == typeof(TestPluginB)); Assert.True(pi.IsEnable); Assert.False(pi.CanInit); Assert.False(pi.CanConfig); pi = loader.PluginList.First(p => p.PluginType == typeof(TestPluginC)); Assert.True(pi.IsEnable); Assert.True(pi.CanInit); Assert.False(pi.CanConfig); pi = loader.PluginList.First(p => p.PluginType == typeof(TestPluginD)); Assert.True(pi.IsEnable); Assert.False(pi.CanInit); Assert.True(pi.CanConfig); Assert.Equal(typeof(TestPluginDOptions), pi.ConfigType); pi = loader.PluginList.First(p => p.PluginType == typeof(TestPluginE)); Assert.True(pi.IsEnable); Assert.True(pi.CanInit); Assert.True(pi.CanConfig); Assert.Equal(typeof(TestPluginEOptions), pi.ConfigType); }