Reflection-based Simple factory

Source: Internet
Author: User

The reflection access modifier is only for the compiler to see that the reflection can be fully manipulated to private
(Gac:gobal Assemble Cache) global assembly caching
The way to get assembly
1.0* loading the corresponding assembly under the current running directory
Assembly Ass1=assembly.load ("Lib")
2.0 loading the assembly under the development directory
Assembly.loadfile () or LoadFrom ()
3.0 load all assemblies in the currently running program domain
assembly[] asss = AppDomain.CurrentDomain.GetAssemblies ();
(Gac:gobal Assemble Cache) global assembly caching
4.0 getting the currently running assembly from the current object
This.gettype (). Assembly;
5.0 the assembly that gets run by type
Type t=typeof (...);
t.assembly;

Several ways to get the type
1.0 get type by typeof method
Type T1 = typeof (Form1);
2.0 get type based on object
Person p = new person ();
Type t2 = P.gettype ();
3.0 get the corresponding type according to the Assembly
Assembly-Assembly.Load ("Lib");
Type t3 =. GetType ("Lib.grilfriend");
4.0 get all the type of the current assembly
type[] T4s =. GetTypes ();
5.0 get all the public type in the current assembly
type[] T5s =. Getexportedtypes ();



Reflection Create objects two ways

1.0 These two ways to create an object, you must have a parameterless constructor in the class
0.0 getting a running assembly
Assembly-Assembly.Load ("Lib");
1.0 Creating through Assemblies
Object obj1 =. CreateInstance ("Lib.grilfriend");
2.0 creating objects with Activator
Get the type corresponding to the class
Type type =. GetType ("Lib.grilfriend");
Object obj2 = Activator.CreateInstance (type);
2.0 How to create an object for a class that does not have a parameterless constructor in the class
0.0 getting a running assembly
Assembly-Assembly.Load ("Lib");
Get the type corresponding to the class
Type type =. GetType ("Lib.grilfriend");
Get constructors in the current class
ConstructorInfo cinfo = type. GetConstructor (new type[] {typeof (String), typeof (int)});
Execute constructor
Object obj3 = Cinfo. Invoke (new object[] {"Liu Yifei", 28});

Use reflection to implement the Notepad hot plug plugin. A Master Notepad program, I come to specify the interface that others develop

Plug-in, placed in the specified folder location
Use reflection to invoke the method in which the case switching function is implemented.
The interface specifies the plugin function name and plugin function .... A read-only property a case-conversion method

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.ComponentModel;4 usingSystem.Data;5 usingSystem.Drawing;6 usingSystem.Linq;7 usingSystem.Text;8 usingSystem.Windows.Forms;9 Ten namespaceNotepad One { A     usingSystem.Reflection; -      Public Partial classForm1:form -     { the          PublicForm1 () -         { - InitializeComponent (); -         } +  -         Private voidForm1_Load (Objectsender, EventArgs e) +         { A             //navigate to the location where the plug-in is stored at             stringBasePath =AppDomain.CurrentDomain.BaseDirectory; -             stringPhypath = BasePath +"iplugs//"; -             //get all plug-in location string traversal -             string[] STRs = System.IO.Directory.GetFiles (Phypath,"*.dll"); -Type Itype =typeof(IPLUGS.IPLUGS01); -             if(STRs. Length >0) in             { -                 foreach(stringIteminchSTRs) to                 { +                     //Get Assembly -Assemblyassembly.loadfile (item); the                     //To get the class array traversal in the assembly *type[] Types =The . GetTypes (); $                     if(Types. Length >0)Panax Notoginseng                     { -                         foreach(varTypeinchtypes) the                         { +                             //the judgment implements the interface A                             if(Itype. IsAssignableFrom (type)) the{//create objects from type +                                 Objectobj =activator.createinstance (type); -                                 //Get Properties $PropertyInfo proinfo = type. GetProperty ("Name"); $                                 //Remove attribute value -                                 ObjectProobj =Proinfo. GetValue (obj); -                                 //give the menu a MenuItem and bind a click event theToolStripMenuItem STMI =NewToolStripMenuItem (proobj. ToString ()); -Stmi. Tag =type;WuyiStmi. Click + =Stmi_click; the                                  This. Ms1. Items.Add (STMI); -                             } Wu                         } -                     } About                 } $             } -         } -  -         voidStmi_click (Objectsender, EventArgs e) A         { +             stringTXT = This. TextBox1.Text; theToolStripMenuItem STMI = Sender asToolStripMenuItem; -Type type = Stmi. Tag asType; $             //Get the method theMethodInfo methinfo = type. GetMethod ("Process",NewType[] {typeof(string) }); the             //Creating Objects the             Objectobj =activator.createinstance (type); the             //Execution Method -             Objectstr = Methinfo. Invoke (obj,New Object[] {txt}); in             //return value is assigned back theTextBox1.Text =Str. ToString (); the  About         } the     } the}
reflection for hot-swappable plug-ins

Reflection-based Simple factory

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.