Exemplo n.º 1
0
        public void LoadPlugins(ILogger <PluginManager> logger)
        {
            string pluginPath = Path.Combine(AppContext.BaseDirectory, "plugins");

            if (!Directory.Exists(pluginPath))
            {
                logger.LogError("Plugin path not found!");
                return;
            }

            foreach (var dir in Directory.GetDirectories(pluginPath))
            {
                try
                {
                    string   name   = Path.GetFileName(dir) + ".dll";
                    Assembly plugin = LoadPlugin(Path.Combine(dir, name));

                    Type[] types          = plugin.GetTypes();
                    Type   adapterType    = null;
                    Type   nodeType       = null;
                    Type   configType     = null;
                    Type   nodeConfigType = null;

                    for (int i = 0; i <= types.Length - 1; i++)
                    {
                        if (typeof(Adapter).IsAssignableFrom(types[i]))
                        {
                            adapterType = types[i];
                        }
                        else if (typeof(Node).IsAssignableFrom(types[i]))
                        {
                            nodeType = types[i];
                        }
                        else if (typeof(IAdapterConfiguration).IsAssignableFrom(types[i]))
                        {
                            configType = types[i];
                        }
                        else if (typeof(INodeConfiguration).IsAssignableFrom(types[i]))
                        {
                            nodeConfigType = types[i];
                        }
                    }

                    if (adapterType == null || nodeType == null || configType == null || nodeConfigType == null)
                    {
                        throw new Exception("Plugin error");
                    }

                    deviceFactory.Register(adapterType, configType);
                    nodeFactory.Register(adapterType, nodeType, nodeConfigType);

                    logger.LogInformation($"Plugin: {adapterType.Name} loaded");
                }
                catch (Exception ex)
                {
                    logger.LogInformation(ex.Message);
                }
            }
        }