Пример #1
0
        void LoadModules()
        {
            //Windows.Storage.StorageFolder localFolder = Windows.Storage.ApplicationData.Current.LocalFolder
            //FileInfo fi = new FileInfo()
            //string modulePath = string.Format(@"{0}\Modules\UI\", AppDomain.CurrentDomain.BaseDirectory);
            string modulePath = string.Format(@"{0}\Modules\UI\", Directory.GetCurrentDirectory());

            string[] strModuleLst = Directory.GetFiles(modulePath, "*.dll", SearchOption.TopDirectoryOnly);
            Assembly plugin;

            Type[] types;
            foreach (string strModule in strModuleLst)
            {
                //C# load dll method
                plugin = Assembly.LoadFrom(strModule);
                types  = plugin.GetTypes();

                foreach (Type t in types)
                {
                    //Check Module interface
                    if (t.GetInterface("IModuleInterface", true) == null)
                    {
                        continue;
                    }
                    //Check is main user control
                    if (!typeof(System.Windows.Controls.UserControl).IsAssignableFrom(t))
                    {
                        continue;
                    }
                    //Check main user control name
                    if (t.Name != "MainControl")
                    {
                        continue;
                    }
                    IModuleInterface iModule = (IModuleInterface)t.InvokeMember(null,
                                                                                BindingFlags.DeclaredOnly |
                                                                                BindingFlags.Public | BindingFlags.NonPublic |
                                                                                BindingFlags.Instance | BindingFlags.CreateInstance,
                                                                                null, null, null);
                    //Check module is support and load into memory
                    if (iModule.isPlatformSupported())
                    {
                        Button btnModule = new Button();
                        btnModule.Name    = string.Format("btn{0}", iModule.moduleName);
                        btnModule.Content = iModule.moduleName;
                        spModules.Children.Add(btnModule);
                        btnModule.Click += BtnModule_Click;;
                        UserControl uc = (UserControl)iModule;
                        uc.Name       = iModule.moduleName;
                        uc.Visibility = Visibility.Collapsed;
                        gdModlueUI.Children.Add(uc);
                    }
                }
                //reset temp.
                plugin = null;
                types  = null;
            }
            //Use Update as main module.
            ShowModule("Update");
        }
        /// <summary>
        /// Search *.dll under Modules\UI\ folder and load into Main process.
        /// </summary>
        void LoadModules()
        {
            //FileInfo fi = new FileInfo()
            string modulePath = string.Format(@"{0}Modules\UI\", AppDomain.CurrentDomain.BaseDirectory);

            string[] strModuleLst = Directory.GetFiles(modulePath, "*.dll", SearchOption.TopDirectoryOnly);
            Assembly plugin;

            Type[] types = null;
            foreach (string strModule in strModuleLst)
            {
                //C# load dll method
                Utilities.Logger(CommonUIConsts.LogUtilityFileName, strModule);
                plugin = Assembly.LoadFrom(strModule);
                try
                {
                    types = plugin.GetTypes();
                }
                catch (Exception ex)
                {
                    Utilities.Logger(CommonUIConsts.LogUtilityFileName, $"Error {ex.Message}");
                    continue;
                }
                foreach (Type t in types)
                {
                    //Check Module interface
                    if (t.GetInterface("IModuleInterface", true) == null)
                    {
                        continue;
                    }
                    //Check is main user control
                    if (!typeof(System.Windows.Controls.UserControl).IsAssignableFrom(t))
                    {
                        continue;
                    }
                    //Check main user control name
                    //if (t.Name != "MainControl")
                    //{
                    //    continue;
                    //}
                    IModuleInterface iModule = (IModuleInterface)t.InvokeMember(null,
                                                                                BindingFlags.DeclaredOnly |
                                                                                BindingFlags.Public | BindingFlags.NonPublic |
                                                                                BindingFlags.Instance | BindingFlags.CreateInstance,
                                                                                null, null, null);
                    //Check module is support and load into memory
                    if (iModule.isPlatformSupported())
                    {
                        Button btnModule = new Button();
                        btnModule.Name    = $"btn{iModule.moduleName}";
                        btnModule.Content = iModule.moduleName;
                        spModules.Children.Add(btnModule);
                        btnModule.Click += BtnModule_Click;
                        UserControl uc = (UserControl)iModule;
                        uc.Name       = iModule.moduleName;
                        uc.Visibility = Visibility.Hidden;
                        gdModlueUI.Children.Add(uc);
                        iModule.initialize();
                    }
                }
                //reset temp.
                plugin = null;
                types  = null;
            }
            //Use Update as main module.
            ShowModule("Update");
        }