Exemple #1
0
        private void LoadPlugin(Assembly assembly)
        {
            try
            {
                foreach (Type type in assembly.GetTypes())
                {
                    if (type.IsSubclassOf(typeof(Plugin)))
                    {
                        NClassEnvironment environment =
                            new NClassEnvironment(Workspace.Default, DocumentManager);
                        Plugin plugin = (Plugin)Activator.CreateInstance(type, environment);
                        PlugIns.Add(plugin);
                    }
                }
            }
            catch (Exception ex)
            {
#if DEBUG
                Debug.WriteLine(ex.Message);
                Debug.WriteLine(ex.StackTrace);
#endif
                OnError?.Invoke(this, new NClassEventArgs {
                    Message = assembly.FullName + "\n" + ex.Message
                });
            }
        }
        public void LoadPlugIn(Assembly a)
        {
            var plugInType = typeof(TPlugIn);
            var allTypes   = a.GetTypes();

            foreach (var type in allTypes)
            {
                if (plugInType.IsAssignableFrom(type) && type.IsClass && !type.IsAbstract)
                {
                    PlugIns.Add(new ApplicationPlugIn <TPlugIn>(this, type));
                }
            }
        }
        /// <summary>
        /// Scan through a given Directory for all PlugIns, and load and Initialize them
        /// </summary>
        /// <param name="directory">Full path of directory to scan</param>
        public void Find(string directory)
        {
            Type typeInterface = typeof(PlugInType);

            try
            {
                PlugIns.Clear();
                if (Directory.Exists(directory) == false)
                {
                    return;
                }
                string[] dllFiles = Directory.GetFiles(directory, "*.dll", SearchOption.TopDirectoryOnly);

                foreach (string file in dllFiles)
                {
                    Assembly pluginAssembly = Assembly.LoadFrom(file);

                    foreach (Type pluginType in pluginAssembly.GetTypes())
                    {
                        Type testType = pluginType.GetInterface(typeInterface.FullName);

                        if ((pluginType.IsSubclassOf(typeInterface) == true) || (testType != null))
                        {
                            PlugInType instance = (PlugInType)Activator.CreateInstance(pluginType);
                            instance.Initialize(Host);

                            PlugIns.Add(instance);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                this.Host.ThrowException(ex);
            }
        }
Exemple #4
0
        /// <summary>
        /// Loads all PlugIns in PlugInFolder directory.
        /// </summary>
        public void LoadPlugIns()
        {
            if (string.IsNullOrEmpty(PlugInFolder) || !Directory.Exists(PlugInFolder))
            {
                throw new ApplicationException("PlugInFoler must be a valid folder path");
            }

            var assemblyFiles = SpsHelper.FindAssemblyFiles(PlugInFolder);
            var plugInType    = typeof(TPlugIn);

            foreach (var assemblyFile in assemblyFiles)
            {
                var allTypes = Assembly.LoadFrom(assemblyFile).GetTypes();
                foreach (var type in allTypes)
                {
                    if (plugInType.IsAssignableFrom(type) && type.IsClass && !type.IsAbstract)
                    {
                        PlugIns.Add(new ApplicationPlugIn <TPlugIn>(this, type));
                    }
                }
            }

            PlugInsLoaded = true;
        }