Exemple #1
0
        private static PluginHandler[] DiscoverPlugins(DirectoryInfo[] probingDirectories)
        {
            // Initialize a list that will contain all plugin types discovered
            List <PluginHandler> pluginHandlers = new List <PluginHandler> ();

            if (probingDirectories != null)
            {
                // Iterate over the probing directories and look for plugins
                foreach (DirectoryInfo directory in probingDirectories)
                {
                    if (directory.Exists)
                    {
                        // Try to load plugins from each dll
                        foreach (FileInfo file in directory.GetFiles("*.dll"))
                        {
                            try
                            {
                                // Load the dll into an assembly
                                Assembly assembly = Assembly.LoadFrom(file.FullName);

                                // Iterate over all types contained in the assembly
                                foreach (Type type in assembly.GetTypes())
                                {
                                    // Only consider public, concrete types that implement IPlugin
                                    if (type.IsPublic && !type.IsAbstract && typeof(IPlugin).IsAssignableFrom(type))
                                    {
                                        PluginHandler pluginHandler = PluginHandler.FromType(type);
                                        if (pluginHandler != null)
                                        {
                                            pluginHandlers.Add(pluginHandler);
                                        }
                                    }
                                }
                            }
                            catch (Exception exception)
                            {
                                Utilities.DefaultTraceSource.TraceEvent(TraceEventType.Error, 0, "A failure occurred while trying to load the {0} Plugin: {1}{2}", file.FullName, Environment.NewLine, exception.ToString());
                            }
                        }
                    }
                }
            }

            // Return the list of plugin types discovered
            return(pluginHandlers.ToArray());
        }