コード例 #1
0
        /// <summary>
        /// Gets the plugins.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <returns></returns>
        public List<IPlugin> GetPlugins( IPluginHost host )
        {
            string localPath = System.IO.Path.GetDirectoryName ( this.GetType ( ).Assembly.Location );
            List<IPlugin> plugins = new List<IPlugin> ( );
            var merged = Paths.Union(Settings.Instance.PluginSettings.DefaultPlaths);
            foreach(string pathName in merged) {

                string tpathName = System.IO.Path.IsPathRooted ( pathName ) ? pathName : System.IO.Path.Combine ( localPath, pathName );

                System.IO.DirectoryInfo path = new System.IO.DirectoryInfo ( tpathName );
                foreach ( var item in path.GetFiles ( "*.dll" ) ) {
                    try {
                        Assembly tasm = AppDomain.CurrentDomain.Load ( AssemblyName.GetAssemblyName ( item.FullName ) );
                        List<Type> types = tasm.GetTypes ( ).ToList<Type> ( );
                        types.Sort ( new Comparison<Type> ( delegate ( Type x, Type y ) {
                            return x.Name.CompareTo ( y.Name );
                        } ) );
                        foreach ( var type in types ) {
                            if ( type.GetInterface ( typeof ( IPlugin ).FullName ) != null && !type.IsInterface ) {
                                try {
                                    ConstructorInfo phCtor = type.GetConstructor ( new Type[] { typeof ( IPluginHost ) } );
                                    IPlugin plugin = null;

                                    PluginInfo pi = GetPluginInfo ( type );

                                    if ( pi == null ) {
                                        pi = new PluginInfo ( );
                                        pi.Enabled = true;
                                        pi.ID = string.Format ( CultureInfo.InvariantCulture, "{0}, {1}", type.FullName, tasm.GetName ( ).Name );
                                        pi.ExecuteOnLoad = false;
                                        pi.ExecuteOnUnload = false;
                                        pi.Name = type.Name;
                                        this.Plugins.Add ( pi );
                                    }

                                    if ( pi.Enabled ) {
                                        if ( !plugins.Contains ( plugin ) ) {
                                            if ( phCtor != null ) {
                                                plugin = Activator.CreateInstance ( type, host ) as IPlugin;
                                            } else {
                                                plugin = Activator.CreateInstance ( type ) as IPlugin;
                                            }
                                            LoadedPlugins.Add ( plugin );
                                            plugins.Add ( plugin );
                                        }
                                    }

                                } catch ( Exception ex ) {
                                    this.LogError ( string.Format ( CultureInfo.InvariantCulture, "Unable to load type {0}: {1}", type.FullName ), ex );
                                }
                            }
                        }
                    } catch ( Exception ex ) {
                        this.LogError ( string.Format ( CultureInfo.InvariantCulture, "Unable to load {0}", item ), ex );
                    }
                }
            }

            return plugins;
        }
コード例 #2
0
        /// <summary>
        /// Gets the plugins.
        /// </summary>
        /// <param name="host">The host.</param>
        /// <returns></returns>
        public List <IPlugin> GetPlugins(IPluginHost host)
        {
            string         localPath = System.IO.Path.GetDirectoryName(this.GetType( ).Assembly.Location);
            List <IPlugin> plugins   = new List <IPlugin> ( );
            var            merged    = Paths.Union(Settings.Instance.PluginSettings.DefaultPlaths);

            foreach (string pathName in merged)
            {
                string tpathName = System.IO.Path.IsPathRooted(pathName) ? pathName : System.IO.Path.Combine(localPath, pathName);

                System.IO.DirectoryInfo path = new System.IO.DirectoryInfo(tpathName);
                foreach (var item in path.GetFiles("*.dll"))
                {
                    try {
                        Assembly    tasm  = AppDomain.CurrentDomain.Load(AssemblyName.GetAssemblyName(item.FullName));
                        List <Type> types = tasm.GetTypes( ).ToList <Type> ( );
                        types.Sort(new Comparison <Type> (delegate(Type x, Type y) {
                            return(x.Name.CompareTo(y.Name));
                        }));
                        foreach (var type in types)
                        {
                            if (type.GetInterface(typeof(IPlugin).FullName) != null && !type.IsInterface)
                            {
                                try {
                                    ConstructorInfo phCtor = type.GetConstructor(new Type[] { typeof(IPluginHost) });
                                    IPlugin         plugin = null;

                                    PluginInfo pi = GetPluginInfo(type);

                                    if (pi == null)
                                    {
                                        pi                 = new PluginInfo( );
                                        pi.Enabled         = true;
                                        pi.ID              = string.Format(CultureInfo.InvariantCulture, "{0}, {1}", type.FullName, tasm.GetName( ).Name);
                                        pi.ExecuteOnLoad   = false;
                                        pi.ExecuteOnUnload = false;
                                        pi.Name            = type.Name;
                                        this.Plugins.Add(pi);
                                    }

                                    if (pi.Enabled)
                                    {
                                        if (!plugins.Contains(plugin))
                                        {
                                            if (phCtor != null)
                                            {
                                                plugin = Activator.CreateInstance(type, host) as IPlugin;
                                            }
                                            else
                                            {
                                                plugin = Activator.CreateInstance(type) as IPlugin;
                                            }
                                            LoadedPlugins.Add(plugin);
                                            plugins.Add(plugin);
                                        }
                                    }
                                } catch (Exception ex) {
                                    this.LogError(string.Format(CultureInfo.InvariantCulture, "Unable to load type {0}: {1}", type.FullName), ex);
                                }
                            }
                        }
                    } catch (Exception ex) {
                        this.LogError(string.Format(CultureInfo.InvariantCulture, "Unable to load {0}", item), ex);
                    }
                }
            }

            return(plugins);
        }