Exemplo n.º 1
0
        private async Task AddExitButton()
        {
            var exitAction = new Action(() =>
            {
                var result = MessageBox.Show("This is also a plugin. Do you want to exit this sample app?", "Exit App?", MessageBoxButtons.YesNo);

                if (result == DialogResult.No)
                {
                    return;
                }

                Application.Exit();
            });

            var exitCatalog = new DelegatePluginCatalog(exitAction, "Exit");
            await exitCatalog.Initialize();

            var exitPlugin = exitCatalog.Single();

            menuStrip1.Items.Add(new ToolStripButton(exitPlugin.Name, null, (sender, args) =>
            {
                dynamic instance = Activator.CreateInstance(exitPlugin);
                instance.Run();
            }));
        }
        public async Task CanConvertParameterToConstructorParameter()
        {
            var rules = new List <DelegateConversionRule>()
            {
                new DelegateConversionRule(info => info.ParameterType == typeof(int), nfo => new ParameterConversion()
                {
                    ToConstructor = true
                })
            };

            var catalog = new DelegatePluginCatalog(new Func <int, Task <bool> >(async i =>
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));
                _testOutputHelper.WriteLine("Hello from test");

                return(true);
            }), rules);

            await catalog.Initialize();

            var pluginType = catalog.Single().Type;

            Assert.Single(pluginType.GetConstructors());
            Assert.Single(pluginType.GetConstructors().Single().GetParameters());
        }
        public async Task CanInitialize()
        {
            var catalog = new DelegatePluginCatalog(new Action(() =>
            {
                _testOutputHelper.WriteLine("Hello from test");
            }));

            await catalog.Initialize();

            var allPlugins = catalog.GetPlugins();

            Assert.NotEmpty(allPlugins);
        }
        public async Task CanConfigurePluginNameFromConstructor()
        {
            var catalog = new DelegatePluginCatalog(new Func <int, Task <bool> >(async i =>
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));
                _testOutputHelper.WriteLine("Hello from test");

                return(true);
            }), "HelloPlugin");

            await catalog.Initialize();

            Assert.Equal("HelloPlugin", catalog.Single().Name);
        }
        public async Task ByDefaultRunMethod()
        {
            var catalog = new DelegatePluginCatalog(new Func <int, Task <bool> >(async i =>
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));
                _testOutputHelper.WriteLine("Hello from test");

                return(true);
            }));

            await catalog.Initialize();

            var pluginType  = catalog.Single().Type;
            var methodInfos = pluginType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

            Assert.Single(methodInfos);
            Assert.Equal("Run", methodInfos.Single().Name);
        }
        public async Task CanConfigureTypename()
        {
            var catalog = new DelegatePluginCatalog(new Func <int, Task <bool> >(async i =>
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));
                _testOutputHelper.WriteLine("Hello from test");

                return(true);
            }), new DelegatePluginCatalogOptions()
            {
                TypeName = "HelloThereType"
            });

            await catalog.Initialize();

            var pluginType = catalog.Single().Type;

            Assert.Equal("HelloThereType", pluginType.Name);
        }
Exemplo n.º 7
0
        public async Task CanTagDelegate()
        {
            var catalog = new DelegatePluginCatalog(new Func <int, bool>(i => true), options: new DelegatePluginCatalogOptions()
            {
                Tags = new List <string>()
                {
                    "CustomTagDelegate"
                }
            });

            await catalog.Initialize();

            var allPlugins = catalog.GetPlugins();

            foreach (var plugin in allPlugins)
            {
                Assert.Equal("CustomTagDelegate", plugin.Tag);
            }
        }
        public async Task ByDefaultNoConstructorParameters()
        {
            var catalog = new DelegatePluginCatalog(new Func <int, Task <bool> >(async i =>
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));
                _testOutputHelper.WriteLine("Hello from test");

                return(true);
            }));

            await catalog.Initialize();

            var pluginType = catalog.Single().Type;

            Assert.Single(pluginType.GetConstructors());

            foreach (var constructorInfo in pluginType.GetConstructors())
            {
                Assert.Empty(constructorInfo.GetParameters());
            }
        }
        public async Task CanConfigureGeneratedMethodNameUsingGenerator()
        {
            var options = new DelegatePluginCatalogOptions()
            {
                MethodNameGenerator = catalogOptions => "MethodGeneratorName"
            };

            var catalog = new DelegatePluginCatalog(new Func <int, Task <bool> >(async i =>
            {
                await Task.Delay(TimeSpan.FromMilliseconds(100));
                _testOutputHelper.WriteLine("Hello from test");

                return(true);
            }), options);

            await catalog.Initialize();

            var pluginType  = catalog.Single().Type;
            var methodInfos = pluginType.GetMethods(BindingFlags.Instance | BindingFlags.Public | BindingFlags.DeclaredOnly);

            Assert.Equal("MethodGeneratorName", methodInfos.Single().Name);
        }