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 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);
        }