コード例 #1
0
        private bool LoadExternalModule(string path)
        {
            bool     foundPlugIn  = false;
            Assembly thePlugInAsm = null;
            var      setting      = new Setting();
            PlugIn   plugin       = new PlugIn();

            try
            {
                // Dynamically load the selected assembly.
                thePlugInAsm            = Assembly.LoadFrom(path);
                setting.AssamblyName    = thePlugInAsm.GetName().ToString();
                setting.AssamblyPath    = path;
                plugin.IncludedAssembly = thePlugInAsm;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                return(foundPlugIn);
            }

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

            // Now, create the object and call IncludeIt() method.
            foreach (Type t in theClassTypes)
            {
                foundPlugIn = true;
                // Use late binding to create the type.
                var itfApp = (IAppFunctionality)thePlugInAsm.CreateInstance(t.FullName, true);
                itfApp.IncludeIt();

                // Show extension  info.
                setting.Extension = DisplayFileFormat(t);
                plugin.Extension  = setting.Extension;
                bool isAdded = false;
                foreach (PlugIn pl in pluginList)
                {
                    if (pl.IncludedAssembly.Equals(plugin.IncludedAssembly))
                    {
                        isAdded = true;
                    }
                }
                if (!isAdded)
                {
                    pluginList.Add(plugin);
                    settings.Add(setting);
                    Setting.AddToFile(settings);
                }
            }
            return(foundPlugIn);
        }
コード例 #2
0
 private void LoadAssamblies()
 {
     settings = Setting.ReadFromFile();
     if (settings == null)
     {
         settings = new List <Setting>();
     }
     else
     {
         foreach (var asmSetting in settings)
         {
             Assembly assem  = Assembly.LoadFrom(asmSetting.AssamblyPath);
             PlugIn   plugin = new PlugIn
             {
                 IncludedAssembly = assem,
                 Extension        = asmSetting.Extension
             };
             pluginList.Add(plugin);
         }
     }
 }