public async Task CanOverrideDefaultNamingOptions() { var options = new AssemblyPluginCatalogOptions() { PluginNameOptions = new PluginNameOptions() { PluginNameGenerator = (nameOptions, type) => type.FullName + "Overridden" } }; var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll"); var catalog2 = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly2.dll", options); await catalog.Initialize(); await catalog2.Initialize(); var catalog1Plugins = catalog.GetPlugins(); foreach (var plugin in catalog1Plugins) { Assert.EndsWith("Modified", plugin.Name); } var catalog2Plugins = catalog2.GetPlugins(); foreach (var plugin in catalog2Plugins) { Assert.EndsWith("Overridden", plugin.Name); } }
public async Task CanUseSelectedHoststDependencies() { // Make sure that the referenced version of JSON.NET is loaded into memory var json = Newtonsoft.Json.JsonConvert.SerializeObject(1); // Make sure that the referenced version of Microsoft.Extensions.Logging is loaded into memory var logging = new Microsoft.Extensions.Logging.LoggerFactory(); var options = new AssemblyPluginCatalogOptions();; options.PluginLoadContextOptions = new PluginLoadContextOptions() { UseHostApplicationAssemblies = UseHostApplicationAssembliesEnum.Selected, HostApplicationAssemblies = new List <AssemblyName>() { typeof(Microsoft.Extensions.Logging.LoggerFactory).Assembly.GetName() } }; var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\JsonOld\netstandard2.0\JsonNetOld.dll", options); await catalog.Initialize(); var oldPlugin = catalog.Single(); dynamic oldPluginJsonResolver = Activator.CreateInstance(oldPlugin); var oldPluginVersion = oldPluginJsonResolver.GetVersion(); var loggerVersion = oldPluginJsonResolver.GetLoggingVersion(); Assert.Equal("3.1.2.0", loggerVersion); Assert.Equal("9.0.0.0", oldPluginVersion); }
public async Task Initialize() { var converter = new DelegateToAssemblyConverter(); var assembly = converter.CreateAssembly(_multicastDelegate, _options); var options = new AssemblyPluginCatalogOptions() { PluginNameOptions = _options.NameOptions }; if (_options.Tags?.Any() == true) { options.TypeFinderOptions = new TypeFinderOptions { TypeFinderCriterias = new List <TypeFinderCriteria> { TypeFinderCriteriaBuilder.Create().Tag(_options.Tags.ToArray()) } }; } _catalog = new AssemblyPluginCatalog(assembly, options); await _catalog.Initialize(); IsInitialized = true; }
public async Task CanUseHostsDependencies() { // Make sure that the referenced version of JSON.NET is loaded into memory var json = Newtonsoft.Json.JsonConvert.SerializeObject(1); _testOutputHelper.WriteLine(json); var options = new AssemblyPluginCatalogOptions { PluginLoadContextOptions = new PluginLoadContextOptions() { UseHostApplicationAssemblies = UseHostApplicationAssembliesEnum.Always } }; var folder1Catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\JsonNew\netstandard2.0\JsonNetNew.dll", options); await folder1Catalog.Initialize(); var folder2Catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\JsonOld\netstandard2.0\JsonNetOld.dll", options); await folder2Catalog.Initialize(); var newPlugin = folder1Catalog.Single(); var oldPlugin = folder2Catalog.Single(); dynamic newPluginJsonResolver = Activator.CreateInstance(newPlugin); var newPluginVersion = newPluginJsonResolver.GetVersion(); dynamic oldPluginJsonResolver = Activator.CreateInstance(oldPlugin); var oldPluginVersion = oldPluginJsonResolver.GetVersion(); Assert.Equal("12.0.0.0", newPluginVersion); Assert.Equal("12.0.0.0", oldPluginVersion); }
public async Task CanConfigureDefaultOptions() { // Make sure that the referenced version of JSON.NET is loaded into memory var json = Newtonsoft.Json.JsonConvert.SerializeObject(1); PluginLoadContextOptions.Defaults.UseHostApplicationAssemblies = UseHostApplicationAssembliesEnum.Always; Action <TypeFinderCriteriaBuilder> configureFinder = configure => { configure.HasName("*JsonResolver"); }; var assemblyPluginCatalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\JsonNew\netstandard2.0\JsonNetNew.dll", configureFinder); var folderPluginCatalog = new FolderPluginCatalog(@"..\..\..\..\..\Assemblies\bin\JsonOld\netstandard2.0", configureFinder); await assemblyPluginCatalog.Initialize(); await folderPluginCatalog.Initialize(); var newPlugin = assemblyPluginCatalog.Single(); var oldPlugin = folderPluginCatalog.Single(); dynamic newPluginJsonResolver = Activator.CreateInstance(newPlugin); var newPluginVersion = newPluginJsonResolver.GetVersion(); dynamic oldPluginJsonResolver = Activator.CreateInstance(oldPlugin); var oldPluginVersion = oldPluginJsonResolver.GetVersion(); Assert.Equal("12.0.0.0", newPluginVersion); Assert.Equal("12.0.0.0", oldPluginVersion); }
public async Task CanInitialize() { var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll"); await catalog.Initialize(); var allPlugins = catalog.GetPlugins(); Assert.NotEmpty(allPlugins); }
public async Task DefaultTagsWithAssemblyCatalogTypeShouldNotDuplicatePlugins() { var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\JsonNew\netstandard2.0\JsonNetNew.dll"); await catalog.Initialize(); Assert.Single(catalog.GetPlugins()); var plugin = catalog.Get(); Assert.Equal(2, plugin.Tags.Count); TypeFinderOptions.Defaults.TypeFinderCriterias.Clear(); }
public async Task CanGetPluginsByAttribute() { var catalog = new AssemblyPluginCatalog(typeof(TypeFinderTests).Assembly, configure => { configure.HasAttribute(typeof(MyPluginAttribute)); }); await catalog.Initialize(); Assert.Equal(2, catalog.GetPlugins().Count); }
public async Task ByDefaultOnlyContainsPublicNonAbstractClasses() { var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll"); await catalog.Initialize(); var allPlugins = catalog.GetPlugins(); var plugin = allPlugins.Single(); Assert.False(plugin.Type.IsAbstract); Assert.False(plugin.Type.IsInterface); }
public async Task CanInitializeWithCriteria() { var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll", configure => { configure.HasName("*Plugin"); }); await catalog.Initialize(); var allPlugins = catalog.GetPlugins(); Assert.Single(allPlugins); }
public async Task CanConfigureDefaultNamingOptions() { var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll"); await catalog.Initialize(); var allPlugins = catalog.GetPlugins(); foreach (var plugin in allPlugins) { Assert.EndsWith("Modified", plugin.Name); } }
public async Task CanGetPluginsByMultipleCriteria() { var catalog = new AssemblyPluginCatalog(typeof(TypeFinderTests).Assembly, configure => { configure.HasAttribute(typeof(MyPluginAttribute)) .IsAbstract(true) .HasName(nameof(AbstractPluginWithAttribute)); }); await catalog.Initialize(); Assert.Single(catalog.GetPlugins()); }
public async Task CanIncludeAbstractClassesUsingCriteria() { var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll", builder => { builder.IsAbstract(true); }); await catalog.Initialize(); var allPlugins = catalog.GetPlugins(); var plugin = allPlugins.Single(); Assert.True(plugin.Type.IsAbstract); }
public async Task Initialize() { var converter = new DelegateToAssemblyConverter(); var assembly = converter.CreateAssembly(_multicastDelegate, _options); var options = new AssemblyPluginCatalogOptions() { PluginNameOptions = _options.NameOptions }; _catalog = new AssemblyPluginCatalog(assembly, options); await _catalog.Initialize(); IsInitialized = true; }
public async Task CanTagAssemblyPlugin() { var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll", null, taggedFilters: new Dictionary <string, Predicate <Type> >() { { "CustomTag", type => true } }); await catalog.Initialize(); var allPlugins = catalog.GetPlugins(); foreach (var plugin in allPlugins) { Assert.Equal("CustomTag", plugin.Tag); } }
public async Task CanTagAssemblyPluginUsingBuilder() { var catalog = new AssemblyPluginCatalog(typeof(TypePlugin).Assembly, builder => { builder.AssignableTo(typeof(TypePlugin)) .Tag("operator"); }); await catalog.Initialize(); var allPlugins = catalog.GetPlugins(); foreach (var plugin in allPlugins) { Assert.Equal("operator", plugin.Tag); } }
private static async Task AssemblyCatalogSample() { Console.WriteLine("Assembly catalog sample"); // 1. Create a new plugin catalog from the current assembly var assemblyPluginCatalog = new AssemblyPluginCatalog(typeof(Program).Assembly, type => typeof(IPlugin).IsAssignableFrom(type)); // 2. Initialize the catalog await assemblyPluginCatalog.Initialize(); // 3. Get the plugins from the catalog var assemplyPlugins = assemblyPluginCatalog.GetPlugins(); foreach (var plugin in assemplyPlugins) { var inst = (IPlugin)Activator.CreateInstance(plugin); inst.Run(); } }
public async Task CanConfigureNamingOptions() { var options = new AssemblyPluginCatalogOptions() { PluginNameOptions = new PluginNameOptions() { PluginNameGenerator = (nameOptions, type) => type.FullName + "Modified" } }; var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\TestAssembly1.dll", options); await catalog.Initialize(); var allPlugins = catalog.GetPlugins(); foreach (var plugin in allPlugins) { Assert.EndsWith("Modified", plugin.Name); } }
public async Task ThrowsIfAssemblyNotFound() { var catalog = new AssemblyPluginCatalog(@"..\..\..\..\..\Assemblies\bin\netstandard2.0\notexists.dll"); await Assert.ThrowsAsync <ArgumentException>(async() => await catalog.Initialize()); }