Beispiel #1
0
        private static void LoadPlugins()
        {
            string strPluginPath = DatabaseConfiguration.DatabaseSettings.PluginLocation;
            string strConfigPath = Path.GetDirectoryName(ApplicationConfiguration.AppConfig.FilePath);

            string[] plugins      = new string[] { };
            Assembly thisAssembly = Assembly.GetCallingAssembly();

            // Add default plugins from our Framework assembly
            LoadedPlugin sql   = new LoadedPlugin(thisAssembly.Location, typeof(SqlProviderPlugin));
            LoadedPlugin odbc  = new LoadedPlugin(thisAssembly.Location, typeof(OdbcProviderPlugin));
            LoadedPlugin oledb = new LoadedPlugin(thisAssembly.Location, typeof(OleDbProviderPlugin));

            _plugins.Add(sql.ProviderPlugin.ProviderType, sql);
            _plugins.Add(odbc.ProviderPlugin.ProviderType, odbc);
            _plugins.Add(oledb.ProviderPlugin.ProviderType, oledb);

            // If the configuration file did not specify a plugin location use the default from the registry
            if (string.IsNullOrEmpty(strPluginPath))
            {
                strPluginPath = DefaultPluginLocation;
            }

            // If the plugins do not exist where the config file was loaded, check the location of the executable
            if (Directory.Exists(Path.Combine(strConfigPath, strPluginPath)) == true)
            {
                strPluginPath = Path.Combine(strConfigPath, strPluginPath);
            }
            else
            {
                strPluginPath = Path.Combine(Path.GetDirectoryName(System.Windows.Forms.Application.ExecutablePath), strPluginPath);
            }

            try
            {
                plugins = Directory.GetFiles(strPluginPath, "*.dll");
            }
            catch { }
            foreach (string plugin in plugins)
            {
                try
                {
                    foreach (LoadedPlugin p in LoadPlugins(Assembly.LoadFile(plugin)))
                    {
                        _plugins.Add(p.ProviderPlugin.ProviderType, p);
                    }
                }
                catch (Exception exception)
                {
                    Exception meaningfulException = exception;

                    while (null != meaningfulException.InnerException)
                    {
                        meaningfulException = meaningfulException.InnerException;
                    }
                    ApplicationLog.WriteError(string.Format("Unable to load plugin {0}", plugin), meaningfulException ?? exception);
                }
            }
        }
Beispiel #2
0
        private static List <LoadedPlugin> LoadPlugins(Assembly loadedAssembly)
        {
            List <LoadedPlugin> loadedPlugins = new List <LoadedPlugin>();

            AssemblyName[] referencedAssemblies    = loadedAssembly.GetReferencedAssemblies();
            bool           loadedAssemblyIsValid   = false;
            AssemblyName   pluginInterfaceAssembly = Assembly.GetAssembly(typeof(IProviderPlugin)).GetName();

            // Check the assembly references to make sure that we can load the plugin
            // by checking the referenced version of this assembly
            foreach (AssemblyName referencedAssembly in referencedAssemblies)
            {
                if (referencedAssembly.Name == pluginInterfaceAssembly.Name)
                {
                    if (referencedAssembly.Version.Major == pluginInterfaceAssembly.Version.Major
                        &&
                        referencedAssembly.Version.Minor == pluginInterfaceAssembly.Version.Minor
                        &&
                        referencedAssembly.Version.Build == pluginInterfaceAssembly.Version.Build)
                    {
                        loadedAssemblyIsValid = true;
                    }
                    break;
                }
                else
                if (loadedAssembly.FullName == pluginInterfaceAssembly.FullName)
                {
                    loadedAssemblyIsValid = true;
                    break;
                }
            }
            if (loadedAssemblyIsValid)
            {
                Type[] loadedAssemblyTypes = loadedAssembly.GetTypes();
                Type   pluginInterfaceType = typeof(IProviderPlugin);

                foreach (Type possiblePluginType in loadedAssemblyTypes)
                {
                    Type[] typeInterfaces = possiblePluginType.GetInterfaces();

                    foreach (Type typeInterface in typeInterfaces)
                    {
                        if (possiblePluginType.IsAbstract == false && pluginInterfaceType.FullName == typeInterface.FullName)
                        {
                            LoadedPlugin plugin = new LoadedPlugin(loadedAssembly.Location, possiblePluginType);
                            loadedPlugins.Add(plugin);
                        }
                    }
                }
            }

            return(loadedPlugins);
        }