コード例 #1
0
        private bool LoadExternalModule(string path)
        {
            bool     foundSnapIn  = false;
            Assembly theSnapInAsm = null;

            try
            {
                // Dynamically load the selected assembly.
                theSnapInAsm = Assembly.LoadFrom(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(foundSnapIn);
            }
            // Get all IAppFunctionality-compatible classes in assembly.
            var theClassTypes = from t in theSnapInAsm.GetTypes()
                                where t.IsClass &&
                                (t.GetInterface("IAppFunctionality") != null)
                                select t;

            // Now, create the object and call DoIt() method.
            foreach (Type t in theClassTypes)
            {
                foundSnapIn = true;
                // Use late binding to create the type.
                IAppFunctionality itfApp =
                    (IAppFunctionality)theSnapInAsm.CreateInstance(t.FullName, true);
                itfApp.DoIt();
                lstLoadedSnapIns.Items.Add(t.FullName);
            }
            return(foundSnapIn);
        }
コード例 #2
0
        private bool LoadExternalModule(string path)
        {
            bool     foundSnapIn = false;
            Assembly asm         = null;

            try
            {
                asm = Assembly.LoadFrom(path);
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return(foundSnapIn);
            }

            var theClassTypes = from t in asm.GetTypes() where t.IsClass && (t.GetInterface("IAppFunctionality") != null) select t;

            foreach (Type t in theClassTypes)
            {
                foundSnapIn = true;

                IAppFunctionality itfApp = (IAppFunctionality)Activator.CreateInstance(t);

                itfApp.DoIt();
                lstLoadedSnapIns.Items.Add(t.FullName);
                DisplayCompanyData(t);
            }
            return(foundSnapIn);
        }
コード例 #3
0
        private void openToolStripMenuItem_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog();

            if (dlg.ShowDialog() == DialogResult.OK)
            {
                foreach (Plugin format in pluginList)
                {
                    if (dlg.FileName.Contains(format.Extension))
                    {
                        MessageBox.Show("Can open this file.");
                        var theClassTypes = from t in format.IncludedAssambly.GetTypes()
                                            where
                                            t.IsClass && (t.GetInterface("IAppFunctionality") != null)
                                            select t;
                        foreach (Type t in theClassTypes)
                        {
                            IAppFunctionality itfApp = (IAppFunctionality)(format.IncludedAssambly.CreateInstance(t.FullName, true));
                            itfApp.OpenFile(dlg.FileName);
                        }
                        return;
                    }
                }
                MessageBox.Show("Can't opent this file");
            }
        }
コード例 #4
0
        private static bool LoadExternalModule(string path)
        {
            bool     foundSnapIn  = false;
            Assembly theSnapInAsm = null;

            try
            {
                // Dynamically load the selected assembly.
                theSnapInAsm = Assembly.LoadFrom(path);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred loading the snapin: {ex.Message}");
                return(foundSnapIn);
            }

            // Get all IAppFunctionality compatible classes in assembly.
            var theClassTypes = from t in theSnapInAsm.GetTypes()
                                where t.IsClass && (t.GetInterface("IAppFunctionality") != null)
                                select t;

            // Now, create the object and call DoIt() method.
            foreach (Type t in theClassTypes)
            {
                foundSnapIn = true;
                // Use late binding to create the type.
                IAppFunctionality itfApp = (IAppFunctionality)theSnapInAsm.CreateInstance(t.FullName, true);
                itfApp?.DoIt();
                //lstLoadedSnapIns.Items.Add(t.FullName);

                // Show company info.
                DisplayCompanyData(t);
            }
            return(foundSnapIn);
        }
コード例 #5
0
        private bool LoadExternalModule(string path)
        {
            bool     foundSnapIn  = false;
            Assembly theSnapInAsm = null;

            try
            {
                //Динамически загрузить выбранную сборку
                theSnapInAsm = Assembly.LoadFrom(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(foundSnapIn);
            }

            //Получить все совместные с IAppFunctionality классы в сборке
            var theClassTypes = from t in theSnapInAsm.GetTypes()
                                where t.IsClass &&
                                (t.GetInterface("IAppFunctionality") != null)
                                select t;

            foreach (Type t in theClassTypes)
            {
                foundSnapIn = true;

                //Использовать позднее связывание для создания экземпляра типа.

                IAppFunctionality itfApp =
                    (IAppFunctionality)theSnapInAsm.CreateInstance(t.FullName, true);
                itfApp.DoIt();
                lstLoadedSnapIns.Items.Add(t.FullName);
            }
            return(foundSnapIn);
        }
コード例 #6
0
ファイル: Form1.cs プロジェクト: zxlong94/LearningDocument
        private bool LoadExternalModule(string path)
        {
            bool     rt = false;
            Assembly a  = null;

            try
            {
                a = Assembly.LoadFrom(path);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(rt);
            }
            var classtype = from t in a.GetTypes() where t.IsClass && (t.GetInterface("IAppFunctionality") != null) select t;

            foreach (Type item in classtype)
            {
                rt = true;
                IAppFunctionality b = (IAppFunctionality)a.CreateInstance(item.FullName, true);
                b.Doit();
                loadsnapins.Items.Add(item.FullName);
                c(item);
            }
            return(rt);
        }
コード例 #7
0
        private static void LoadExternalModule(string path)
        {
            bool found = false;

            try {
                Assembly asm   = Assembly.LoadFrom(path);
                Type[]   types = asm.GetTypes();
                foreach (Type t in types)
                {
                    Type iAppFuncType = t.GetInterface("ModuleDefinition.IAppFunctionality");
                    if (iAppFuncType != null && t.IsClass)
                    {
                        found = true;
                        IAppFunctionality app = (IAppFunctionality)asm.CreateInstance(t.FullName, true);
                        app.DoIt();
                        PrintCompanyInformation(t);
                    }
                }
            } catch (Exception ex) {
                Console.WriteLine("Error!");
                Console.WriteLine(ex);
            }

            if (!found)
            {
                Console.WriteLine("Module is incompatible!");
            }
        }
コード例 #8
0
ファイル: MainForm.cs プロジェクト: simple555a/DotNetAppDev
        private bool LoadExternalModule(string pathToAssembly)
        {
            bool     foundSnapIn = false;
            Assembly theSnapInAsm;

            try
            {
                // Динамическая загрузка выбранной сборки.
                theSnapInAsm = Assembly.LoadFrom(pathToAssembly);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(false);
            }

            // Получение информации обо всех совместимых с IAppFunctionality классах в сборке.
            var theClassTypes = from t in theSnapInAsm.GetTypes()
                                where t.IsClass && (t.GetInterface("IAppFunctionality") != null)
                                select t;

            // Создание объекта и вызов метода DoIt().
            foreach (Type t in theClassTypes)
            {
                foundSnapIn = true;
                // Использование позднего связывания для создания типа.
                IAppFunctionality itfApp =
                    (IAppFunctionality)theSnapInAsm.CreateInstance(t.FullName, true);
                itfApp.DoIt();
                snapInListBox.Items.Add(t.FullName ?? throw new InvalidOperationException());
                // Отображение информации о компании.
                DisplayCompanyData(t);
            }
            return(foundSnapIn);
        }
コード例 #9
0
ファイル: Program.cs プロジェクト: fdaleks/ExtendableApp
        private static bool LoadExternalModule(string path)
        {
            bool     foundSnapIn  = false;
            Assembly theSnapInAsm = null;

            try
            {
                theSnapInAsm = Assembly.LoadFrom(path);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occured loading the snaping: {ex.Message}");
                return(foundSnapIn);
            }

            var theClassTypes = from t in theSnapInAsm.GetTypes()
                                where t.IsClass && (t.GetInterface("IAppFunctionality") != null)
                                select t;

            foreach (Type t in theClassTypes)
            {
                foundSnapIn = true;
                IAppFunctionality itfApp = (IAppFunctionality)theSnapInAsm.CreateInstance(t.FullName, true);
                itfApp?.DoIt();
                DisplayCompanyData(t);
            }
            return(foundSnapIn);
        }
コード例 #10
0
ファイル: Program.cs プロジェクト: dakazia/apress
        private static void LoadExternalModule(string assemblyName)
        {
            Assembly theSnapInAsm = null;

            try
            {
                // Dynamically load the selected assembly.
                theSnapInAsm = Assembly.LoadFrom(assemblyName);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"An error occurred loading the snapin: {ex.Message}");
                return;
            }

            // Get all IAppFunctionality compatible classes in assembly.
            var theClassTypes =
                theSnapInAsm.GetTypes()
                .Where(t => t.IsClass && (t.GetInterface("IAppFunctionality") != null))
                .ToList();

            if (!theClassTypes.Any())
            {
                Console.WriteLine("Nothing implements IAppFunctionality!");
            }

            // Now, create the object and call DoIt() method.
            foreach (Type t in theClassTypes)
            {
                // Use late binding to create the type.
                IAppFunctionality itfApp = (IAppFunctionality)theSnapInAsm.CreateInstance(t.FullName, true);
                itfApp?.DoIt();
                //lstLoadedSnapIns.Items.Add(t.FullName);

                // Show company info.
                DisplayCompanyData(t);
            }
        }
コード例 #11
0
ファイル: ExtenableApp.cs プロジェクト: ch3rag/C-Sharp
        private static bool LoadExternalModule(string filePath)
        {
            bool     foundSnapIn = false;
            Assembly snapInAsm   = null;

            try {
                snapInAsm = Assembly.LoadFrom(filePath);
            } catch (Exception ex) {
                Console.WriteLine("Error while loading the assembly!");
                return(false);
            }

            (from type in snapInAsm.GetTypes() where type.IsClass && (type.GetInterface("IAppFunctionality") != null) select type).ToList().ForEach(x => {
                foundSnapIn           = true;
                IAppFunctionality app = (IAppFunctionality)snapInAsm.CreateInstance(x.FullName, true);
                if (app != null)
                {
                    app.DoIt();
                }
                DisplayCompanyInfo(x);
            });

            return(foundSnapIn);
        }