Exemple #1
0
 public void LoadPlugin(Assembly assembly, PluginLoaderOptions options)
 {
     try
     {
         var loadingEvent = new PluginLoadingEventArgs {
             PluginAssembly = assembly
         };
         options?.RaiseLoading(this, loadingEvent);
         if (loadingEvent.Skip)
         {
             return;
         }
         var attrs = assembly.GetCustomAttributes <T>(); // Exact match
         var attr  = attrs.FirstOrDefault(t => t.GetType() == typeof(T)) ?? attrs.FirstOrDefault();
         if (attr is null)
         {
             throw new PluginException($"The plugin assembly is not decorated with {typeof(T).FullName}.");
         }
         var entryPoint = attr.OnInitialization();
         if (entryPoint is null)
         {
             throw new PluginException($"The plugin entry-point couldn't be initialized.");
         }
         entryPoint.Setup(new ServiceCollectionExtender(_services, _defs));
         options?.RaiseLoaded(this, loadingEvent);
     }
     catch (Exception ex)
     {
         options?.RaiseError(this, new PluginErrorEventArgs()
         {
             PluginAssembly = assembly, Error = ex
         });
     }
 }
Exemple #2
0
 /// <summary>
 /// Searches for a compatible entry-point in every compatible plugin file found in the specified directory, and call its <see cref="IEntrypoint.Setup">Setup</see> method.
 /// </summary>
 /// <param name="path">Either the path of a directory or an existing file.</param>
 /// <param name="options">Options for the plugins loader.</param>
 /// <param name="pattern">Pattern to search for in the name of the files within the directory, if a directory path was supplied.</param>
 public static void LoadPlugins(this IExpandR expandr, string path, PluginLoaderOptions options = default, string pattern = DefaultDirPattern)
 {
     if (File.Exists(path))
     {
         expandr.LoadPlugin(new FileInfo(Path.GetDirectoryName(path)), options);
     }
     else
     {
         expandr.LoadPlugins(new DirectoryInfo(path), options, pattern);
     }
 }
Exemple #3
0
        /// <summary>
        /// Searches for a compatible entry-point in the specified file, and call its <see cref="IEntrypoint.Setup">Setup</see> method.
        /// </summary>
        /// <param name="file">The plugin assembly binary, to be loaded via reflection.</param>
        /// <param name="options">Options for the plugins loader.</param>
        public static void LoadPlugin(this IExpandR expandr, FileInfo file, PluginLoaderOptions options = default)
        {
            Assembly asm;

            try
            {
                asm = Assembly.LoadFrom(file.FullName);
            }
            catch (Exception ex)
            {
                options?.RaiseError(expandr, new PluginErrorEventArgs()
                {
                    Error = new PluginFileException(file, $"Invalid assembly file: {ex.Message}", ex)
                });
                return;
            }
            expandr.LoadPlugin(asm, options);
        }
Exemple #4
0
 /// <summary>
 /// Searches for a compatible entry-point in every compatible plugin file found in the specified directory, and call its <see cref="IEntrypoint.Setup">Setup</see> method.
 /// </summary>
 /// <param name="directory">The plugin assemblies directory, that are loaded via reflection after being sorted alphabetically.</param>
 /// <param name="options">Options for the plugins loader.</param>
 /// <param name="pattern">Pattern to search for in the name of the files within the directory.</param>
 public static void LoadPlugins(this IExpandR expandr, DirectoryInfo directory, PluginLoaderOptions options = default, string pattern = DefaultDirPattern)
 {
     Directory.CreateDirectory(directory.FullName);
     foreach (var file in directory.GetFiles(pattern).OrderBy(dllPath => dllPath.Name))
     {
         expandr.LoadPlugin(file, options);
     }
 }