コード例 #1
0
        protected void watcher_Deleted(object sender, FileSystemEventArgs e)
        {
            PluginAssembly cachedAssembly = null;

            if (e.ChangeType == WatcherChangeTypes.Deleted)
            {
                //Remove from cache
                if (assemblies.Value.ContainsKey(e.FullPath.Trim().ToLower()))
                {
                    assemblies.Value.TryRemove(e.FullPath.Trim().ToLower(), out cachedAssembly);
                    if (AssemblyRemovedFromCache != null)
                    {
                        AssemblyRemovedFromCache(this, new PluginEventArgs(e.FullPath.Trim().ToLower()));
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Retrieving all class instances from assembly which are imlementing IPlugin interface and inheriting T
        /// (type T must implement IPlugin in order to have its type retrieved)
        /// </summary>
        /// <typeparam name="T">Type of plugin base implementing IPlugin interface</typeparam>
        /// <param name="PluginName">Plugin assembly filename or full path to plugin assembly</param>
        /// /// <param name="Subfolder">Optional subfolder name where plugin assembly is located inside plugins folder</param>
        /// <returns></returns>
        public IEnumerable <T> GetPlugin <T>(string PluginName, string Subfolder = null) where T : Plugin, IPlugin
        {
            string             pluginPath     = string.Empty;
            PluginAssembly     loadedAssembly = null;
            IEnumerable <Type> pluginTypes    = null;

            if (File.Exists(PluginName))
            {
                pluginPath = new DirectoryInfo(PluginName).FullName.ToLower();
                if (pluginPath.Contains(this.PluginsFolder))
                {
                    throw new PluginFileNotInPluginsFolderException(string.Format(Strings.PluginFileNotInPluginsFolderException, pluginPath, this.PluginsFolder));
                }
            }
            else
            {
                if (string.IsNullOrWhiteSpace(Subfolder))
                {
                    pluginPath = Path.Combine(this.PluginsFolder, string.Format("{0}{1}", PluginName, !PluginName.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) ? ".dll" : string.Empty)).Trim().ToLower().ToString();
                }
                else
                {
                    pluginPath = Path.Combine(this.PluginsFolder, Subfolder, string.Format("{0}{1}", PluginName, !PluginName.EndsWith(".dll", StringComparison.InvariantCultureIgnoreCase) ? ".dll" : string.Empty)).Trim().ToLower().ToString();
                }
            }

            if (!assemblies.Value.ContainsKey(pluginPath))
            {
                loadedAssembly = new PluginAssembly(DateTime.Now, LoadAssemblyFromFilesystem(pluginPath));
                pluginTypes    = GetPluginTypes <T>(loadedAssembly.Assembly);
                //Do not add to cache if cache policy not defined or assembly does not have IPlugin classes
                if (this.CachePolicy != null && pluginTypes != null && pluginTypes.Any())
                {
                    assemblies.Value.AddOrUpdate(pluginPath, loadedAssembly, (key, oldValue) => loadedAssembly);
                    if (AssemblyLoaded != null)
                    {
                        AssemblyLoaded(this, new PluginEventArgs(pluginPath));
                    }
                }
            }
            else
            {
                //Update load time if sliding expiration
                if (this.CachePolicy.SlidingExpiration)
                {
                    assemblies.Value.AddOrUpdate(pluginPath, new PluginAssembly(DateTime.Now, loadedAssembly.Assembly), (key, oldValue) => new PluginAssembly(DateTime.Now, loadedAssembly.Assembly));
                }
                else
                {
                    assemblies.Value.TryGetValue(pluginPath, out loadedAssembly);
                }
            }

            if (pluginTypes == null)
            {
                pluginTypes = GetPluginTypes <T>(loadedAssembly.Assembly);
            }

            foreach (var type in pluginTypes)
            {
                /*
                 * var ctor = type.GetConstructor(new Type[] { typeof(string) });
                 * var plugin = ctor.Invoke(new object[] { pluginPath });
                 * yield return plugin as T;
                 */
                yield return(Activator.CreateInstance(type, new object[] { pluginPath }) as T);
            }
        }