Modular development and module development

Source: Internet
Author: User

Modular development and module development

These two days I saw a colleague's little tool, which was developed in modular mode, also known as plug-in development, and implemented through reflection + interface. I felt quite good. I just learned about it and wrote a small Demo, which is recorded here.

I. Write Interface Class

The interface class is the basis of all modules, because the main program is to find the module through reflection to find the relevant projects that inherit this interface, that is, the DLL file that later contains the inherited interface class.

This interface class contains the following attributes:

Tool Name (required), whether to bring up, foreground color, background color, tool startup method (required)

Public interface IToolsInterface {// <summary> // obtain the Tool Name /// </summary> string ToolName {get ;} /// <summary> /// whether to bring up /// </summary> bool IsPopUp {get ;} /// <summary> /// foreground color /// </summary> Brush ForgroundBrush {get ;} /// <summary> /// background color /// </summary> Brush BackgroundBrush {get ;} /// <summary> /// tool start method /// </summary> /// <returns> </returns> FrameworkElement RunToolApplication ();}

Ii. Write the main form

The so-called main form is the bearer of each module, because each module is a UserControl, and the form is required to carry it.

There are about two methods required: Find the directory level and create the corresponding module.

1. Find the directory level

Because the module is finally generated to a location, and then let the main program to search, so you need a search method to find

/// <Summary> /// search for all the last subdirectories in the specified directory /// </summary> /// <param name = "dir"> directory to be searched </param> /// <param name = "dirList"> Search Result List </param> /// <param name = "system"> whether to include the system directory </param> /// <param name = "hidden"> whether to include a hidden directory </param> public static void GetEndDirectories (DirectoryInfo dir, list <DirectoryInfo> dirList, bool system = false, bool hidden = false) {try {// returns the directory set DirectoryInfo [] dirSub = dir. getDirectories (); If (dirSub. length = 0) {// if no sub-directory exists, add it to the list dirList. add (dir); return;} foreach (DirectoryInfo subItem in dirSub) {// skip the system directory if (! System & (subItem. Attributes & FileAttributes. System) = FileAttributes. System) {continue;} // skip the hidden directory if (! Hidden & (subItem. attributes & FileAttributes. hidden) = FileAttributes. hidden) {continue;} // recursive GetEndDirectories (subItem, dirList);} catch (Exception ex) {MessageBox. show ("failed to get directory level. "+ Ex. Message );}}

2. Create a module

When a DLL exists, a module is generated, and two DLL modules are required, and so on ......

/// <Summary> /// create a function button /// </summary> /// <param name = "toolsInterface"> </param> /// <returns> </returns> private UIElement CreateFunction (IToolsInterface toolsInterface) {Button btn = new Button (); btn. click + = Btn_Click; btn. content = toolsInterface. toolName; btn. width = 100; btn. height = 50; btn. margin = new Thickness (5, 0, 0, 0); btn. background = toolsInterface. backgroundBrush; btn. foreground = ToolsInterface. ForgroundBrush; btn. Tag = toolsInterface; return btn;} private void Btn_Click (object sender, RoutedEventArgs e) {Button btn = sender as Button; if (btn! = Null) {IToolsInterface toolsInterface = btn. Tag as IToolsInterface; if (toolsInterface! = Null) {FrameworkElement control = toolsInterface. runToolApplication (); gUc. children. clear (); gUc. children. add (control);} else {MessageBox. show ("instantiation interface failed") ;}} else {MessageBox. show ("instantiation button failed ");}}

3. Load modules

Private void LoadWidgets () {string applicationPath = AppDomain. CurrentDomain. SetupInformation. ApplicationBase; string dirPath = applicationPath + "Widgets/"; if (! Directory. Exists (dirPath) {MessageBox. Show ("related Widgets Folder Information is not found. Check whether the related folder Exists. "); Return;} DirectoryInfo dir = new DirectoryInfo (dirPath); List <DirectoryInfo> lastDirNameList = new List <DirectoryInfo> (); Tools. fileToolsHelper. getEndDirectories (dir, lastDirNameList); foreach (DirectoryInfo item in lastDirNameList) {WrapPanel wrapPanel = null; TabItem tabItem = Tools. UCToolsHelper. createTabByDirName (item. name, tcToolkClass); wrapPanel = Tools. UCToolsHelper. createWrapPanel (tab Item); string [] dllFilesPath = Directory. getFiles (item. fullName ,"*. dll "); foreach (string dllPath in dllFilesPath) {Assembly assembly = Assembly. loadFile (dllPath); Type [] types = assembly. getExportedTypes (); foreach (Type type in types) {if (typeof (IToolsInterface ). isAssignableFrom (type )&&! Type. IsAbstract) {IToolsInterface toolInterface = Activator. CreateInstance (type) as IToolsInterface; if (toolInterface! = Null) {wrapPanel. Children. Add (CreateFunction (toolInterface ));}}}}}}

3. Write the corresponding module

I haven't written any small things recently. I moved the two original Winform items directly into WPF, changed the form to UserControl, and added an additional class, used to implement the interface mentioned in the first part.

One of the gadgets here: http://www.cnblogs.com/ZXdeveloper/p/5682230.html

The basic thing is not moving, but a FunctionHelper is added to implement the interface. Note that it must be Public, otherwise it cannot be queried.

Public class FunctionHelper: IToolsInterface {public Brush BackgroundBrush {get {return new SolidColorBrush (Colors. lightBlue) ;}} public Brush ForgroundBrush {get {return new SolidColorBrush (Colors. yellowGreen) ;}} public bool IsPopUp {get {return false ;}} public string ToolName {get {return "method query tool" ;}} public FrameworkElement RunToolApplication () {return new SearchUC ();}}

4. Take a look

This process is probably not very difficult, facilitating later development.

There are still a lot of imperfections in the DEMO, so I will keep improving it later.

DEMO

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.