Method of Reflection Action Notepad add plugin

Source: Internet
Author: User



This article is mainly about the dynamic acquisition of plug-ins through reflection.






First, all plugins must adhere to the specification, which is an interface defined as follows:


1. Public interface ieditplus / / the interface must be public, otherwise it cannot be referenced externally
2 {
3 string name {get;} / / the name of the plug-in
4 void changestring (textbox TB); / / method to change string
5} 


The interface of the main window





The main idea is to find all of the. dll files under the/debug/lib folder, and manipulate them by means of reflection. (Add Sub-menu under menu edit)
1   private void Form1_Load(object sender, EventArgs e)
2 {
3 / / search / bin / *. DLL
4 / / get the current assembly
5             Assembly ass = Assembly.GetExecutingAssembly();
6             string dirPath = Path.Combine(Path.GetDirectoryName(ass.Location), "lib");
7 string [] filepaths = directory.getfiles (dirpath, "*. DLL"); / / search all DLL files in the folder of the current assembly
8             foreach (var filePath in filePaths)
9             {
10                 Assembly assdll = Assembly.LoadFile(filePath);
11                 Type[] tps = assdll.GetTypes();
12                 foreach (Type tp in tps)
13                 {
14                     if (typeof(IEditPlus).IsAssignableFrom(tp) && !tp.IsAbstract)
15                     {
16 ieditplus EP = (ieditplus) activator. Createinstance (TP); / / get the object and convert it to an interface object
17 toolstripitem TSI = TMS. Dropdownitems. Add (EP. Name); / / load submenu for edit menu
18 tsi.tag = EP; / / save this object as a tag of TSI
19 TSI. Click + = tsi_click; / / add a click event to this sublist
20                     }
21                 }
22             }
23}
24         void tsi_Click(object sender, EventArgs e)
25 {
26 toolstripitem TSI = sender as toolstripitem; / / convert sender display to toolstripitem
27 If (TSI! = null) / / make sure TSI is not null, because sender is an accepted object
28             {
29 / / to operate the changestring method of ieditplus in the event, you need to remove it from the tag
30                 IEditPlus ep = tsi.Tag as IEditPlus;
31                 ep.ChangeString(textBox1);
32             }
Thirty-three
34} 


lowercase to uppercase DLL file code ---Place the generated DLL code under the/debug/lib file of the main program


1 public class lowercase to uppercase: ieditplus
2 {
3         public void ChangeString(System.Windows.Forms.TextBox tb)
4 {
5             tb.Text = tb.Text.ToUpper();
6}
7         public string Name
8 {
9 get {return "lowercase to uppercase";}
10}
11} 


DLL code that changes the font ---Place the generated DLL code under the/debug/lib file of the main program



First add a form, the interface






Add the code for the class:


1 public void ChangeString(System.Windows.Forms.TextBox tb)
2 {
3             FontFm ff = new FontFm(tb);
4             ff.ShowDialog();
5}
6         public string Name
7 {
8 get {return "font";}
9} 


Code for the form:


1  public FontFm()
2 {
3             InitializeComponent();
4}
5         public FontFm(TextBox tb):this()
6 {
7            this._tb = tb;
8}
9 private textbox? TB; / / to operate textbox TB, add fields and constructors
10         private void button1_Click(object sender, EventArgs e)
11 {
12_tb. Font = new font (cbmfont. Text, float. Parse (cbmsize. Text)); / / change font
13 this. Close(); / / close the form
14} 





I think, the reflection and the interface together, the ROD!!!






Method of Reflection Action Notepad add plugin


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.