Ejemplo n.º 1
0
        public PluginBase Build(RepositorySettings settings)
        {
            _metadataList = _pluginLoader.LoadPlugins(_pluginRepository, _pluginTypes);

            var metadata = (from m in _metadataList
                            where m.PluginId == settings.PluginId
                            select m).SingleOrDefault();

            if (metadata == null)
            {
                return(new EmptyPlugin());
            }
            else if (_pluginTypes.ContainsKey(metadata.Dll))
            {
                var type   = _pluginTypes[metadata.Dll];
                var plugin = Activator.CreateInstance(type);

                var p = plugin as PluginBase;
                p?.Initialise(settings, _logger);
                return(p);
            }
            else
            {
                throw new NotSupportedException($"Cannot instanciate the plugin '{metadata.Dll}'. Did you forget to load the plugins?");
            }
        }
Ejemplo n.º 2
0
        public IPlugin Build(string name)
        {
            _metadataList = _pluginLoader.LoadPlugins(PluginRepository, _pluginTypes);
            name          = name.ToLower();

            var metadata = GetMetadataList(name).FirstOrDefault();

            if (metadata == null)
            {
                var p = new EmptyPlugin();
                p.Initialise(_pluginContext);
                return(p);
            }
            else if (_pluginTypes.ContainsKey(metadata.Dll))
            {
                var type   = _pluginTypes[metadata.Dll];
                var plugin = Activator.CreateInstance(type);

                var p = plugin as PluginBase;
                p?.Initialise(_pluginContext);
                return(p);
            }
            else
            {
                throw new NotSupportedException($"Cannot instanciate the plugin '{metadata.Dll}'. Did you forget to load the plugins?");
            }
        }
Ejemplo n.º 3
0
        /// <summary>
        /// Find a plugin by name. The plugin must implement a Name property.
        ///     public string Name { get; set; }
        /// The Dependency resolver is only loaded for the found plugin.
        /// </summary>
        /// <param name="pluginName">The plugin name</param>
        /// <param name="dir">The directory to search</param>
        /// <returns>A found plugin of type T.</returns>
        public T FindPlugin(string pluginName, string dir, IPluginObjectCreator <T> pluginObjectCreator = null)
        {
            if (string.IsNullOrWhiteSpace(pluginName))
            {
                return(default(T));
            }
            if (string.IsNullOrWhiteSpace(dir))
            {
                return(default(T));
            }
            _Logger?.WriteLine(PluginLoaderLogLevel.Info, $"Attempting to find plugin: {pluginName}; from path: {dir}");
            var plugins = _PluginLoader.LoadPlugins(Directory.GetFiles(dir, DllExtension));

            if (plugins == null || !plugins.Any())
            {
                return(default(T));
            }
            pluginObjectCreator = pluginObjectCreator ?? _PluginObjectCreator;
            foreach (var plugin in plugins)
            {
                // Find by attribute (New preferred method)
                foreach (var type in plugin.PluginTypes)
                {
                    var pluginAttribute = type.GetCustomAttribute <PluginAttribute>();
                    if (pluginAttribute != null && pluginName.Equals(pluginAttribute.Name, StringComparison.InvariantCultureIgnoreCase))
                    {
                        return(plugin.CreatePluginObject(type, pluginObjectCreator));
                    }
                }

                // Find by a Named property on instance of class (legacy way)
                var pluginObjects = plugin.CreatePluginObjects(pluginObjectCreator);
                if (pluginObjects == null)
                {
                    continue;
                }
                foreach (var obj in pluginObjects)
                {
                    dynamic namedObj = obj;
                    if (namedObj != null)
                    {
                        if (namedObj.Name.Equals(pluginName, StringComparison.InvariantCultureIgnoreCase))
                        {
                            return(obj);
                        }
                    }
                }
            }
            return(default(T));
        }