Quantcast
Channel: Visual C# forum
Viewing all articles
Browse latest Browse all 31927

Call plugin methods dynamically - Managed Extensibilty Framework (MEF)

$
0
0

Hi 

I have been looking at Steven Hollidges' MEF example here

Is there a way to call any method in the plugin dll that has been imported by passing the name of the method as string with parameters?

Here is the sample code showing the Calculate method being called:

                // Production code would usually load the directory from config
                var catalog = new DirectoryCatalog(@"..\..\..\Plugins");
                var container = new CompositionContainer(catalog);
                var pluginRepository = new PluginRepository()
                {
                    CalculationServices = container.GetExportedValues<ICalculationService>()
                };

                foreach (var calculationService in pluginRepository.CalculationServices)
                {
                    const int number1 = 10;
                    const int number2 = 20;
                    int result = calculationService.Calculate(number1, number2);

                    string output = string.Format("calculationService.Calculate({0},{1}) = {2}", number1, number2, result);

                    Console.WriteLine(output);
                }



Viewing all articles
Browse latest Browse all 31927

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>