I have been reading information about assembly loading and reflection recently, so I will record what I learned here so that I can review it later, for reference by friends in the garden.
I. Assembly Loading
When the JIT compiler compiles the IL code at a cost, it will check the types referenced in the IL code. During running, the JIT compiler uses the TypeRef and AssemblyRef metadata tables of the Assembly to determine which Assembly defines the referenced type, and then the JIT compiler loads the corresponding assembly into the AppDomain, internally, CLR uses System. reflection. load the static method of the Assembly class to try to Load an Assembly. However, if we want to dynamically Load an Assembly, we can use the Assembly Load method to dynamically Load the Assembly. The Assembly class also provides other methods to Load the Assembly, there are LoadFrom (string path), LoadFile (stringassemblyFile), etc., the use and interpretation of specific methods can refer to the description in MSDN: http://msdn.microsoft.com/zh-cn/library/xbe1wdx9
Ii. reflection mechanism
. Net reflection parses the metadata in the Assembly during running, and obtains information about the type of members, including fields, constructors, methods, attributes, and events.
Dynamically load an assembly and obtain members of the type
Put the following classes in a class library project and compile and generate an assembly, for example, ClassLibrary1.dll. Assume that the dll is placed under the root directory of the d disk)
650) this. width = 650; "id =" code_img_closed_1a8d9f04-324f-450a-82a2-af429dc1af08 "class =" code_img_closed "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1F2321927-0.gif "/> 650) this. width = 650; "style =" display: none "id =" code_img_opened_1a8d9f04-324f-450a-82a2-af429dc1af08 "class =" code_img_opened "alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1F2321401-1.gif "/> View Code
1 public class ReflectTestClass 2 { 3 public string name; 4 public int age; 5 public string Name 6 { 7 get { return name; } 8 set { name = value; } 9 }10 11 public int Age12 {13 get { return age; }14 set { age = value; }15 }16 17 /// <summary>18 /// No Paramter Constructor19 /// </summary>20 public ReflectTestClass()21 { 22 }23 24 /// <summary>25 /// Constructor with Parameter26 /// </summary>27 /// <param name="name"></param>28 /// <param name="age"></param>29 public ReflectTestClass(string names,int ages)30 {31 this.name = names;32 this.age = ages;33 }34 35 public string writeString(string name)36 {37 return "Welcome " + name;38 }39 40 public static string WriteName(string name)41 {42 return "Welcome "+name +" Come here";43 }44 45 public string WirteNopara()46 {47 return "The method is no parameter ";48 }49 }
Then a console program is created to dynamically load the members of the above-generated assembly and output type. The code is described in detail.
650) this. width = 650; "alt =" Copy code "src =" http://www.bkjia.com/uploads/allimg/131228/1F2326329-2.gif "/>
Class Program {static void Main (string [] args) {Assembly ass; Type [] types; Type typeA; object obj; try {// load the Assembly from the local machine and obtain the type information from the Assembly through reflection, and call method ass = Assembly. loadFrom (@ "D: \ ClassLibrary1.dll"); types = ass. getTypes (); foreach (Type type in types) {Console. writeLine ("Class Name is" + type. fullName); Console. writeLine ("Constructor Information"); Console. writeLine ("-----------------------"); // obtain the ConstructorInfo [] myconstructors = type. getConstructors (); ShowMessage <ConstructorInfo> (myconstructors); Console. writeLine ("Fields Information"); Console. writeLine ("-----------------------"); // obtain the type field information FieldInfo [] myfields = type. getFields (); ShowMessage <FieldInfo> (myfields); Console. writeLine ("All Methods Information"); Console. writeLine ("-----------------------"); // obtain method information MethodInfo [] myMethodInfo = type. getMethods (); ShowMessage <MethodInfo> (myMethodInfo); Console. writeLine ("All Properties Information"); Console. writeLine ("-----------------------"); // obtain the Property Information PropertyInfo [] myproperties = type. getProperties (); ShowMessage <PropertyInfo> (myproperties);} // use the namespace + class name to obtain the type typeA = ass. getType ("ClassLibrary1.ReflectTestClass"); // obtain the method name MethodInfo method = typeA. getMethod ("writeString"); // create an instance obj = ass. createInstance ("ClassLibrary1.ReflectTestClass"); string result = (String) method. invoke (obj, new string [] {"Tom"}); Console. writeLine ("Invoke Method With Parameter"); Console. writeLine ("-----------------------"); Console. writeLine (result); Console. writeLine ("-----------------------"); Console. writeLine (); method = typeA. getMethod ("WriteName"); result = (string) method. invoke (null, new string [] {"Tom"}); Console. writeLine ("Invoke Static Method with Parameter"); Console. writeLine ("-----------------------"); Console. writeLine (result); Console. writeLine ("-----------------------"); Console. writeLine (); method = typeA. getMethod ("WirteNopara"); Console. writeLine ("Invoke Method with NOParameter"); result = (string) method. invoke (obj, null); Console. writeLine ("-----------------------"); Console. writeLine (result); Console. writeLine ("-----------------------");} catch (FileNotFoundException ex) {Console. writeLine (ex. message);} Console. readLine ();} /// <summary> // display the array information /// </summary> /// <typeparam name = "T"> </typeparam> /// <param name = "OS"> </param> public static void ShowMessage <T> (T [] array) {foreach (T member in array) {Console. writeLine (member. toString ();} Console. writeLine ("-----------------------"); Console. writeLine ();}}650) this. width = 650; "alt =" Copy code "src =" http://www.bkjia.com/uploads/allimg/131228/1F2326329-2.gif "/>
Filter returned member types
You can call the GetMembers, GetFields, GetMethods, GetProperties, or GetEvenents methods of Type to query members of a Type. When you call any of the above methods, you can pass an instance of the System. Reflection. BindingFlags Enumeration type. This enumeration type is used to filter the members returned by these methods. For information about members in this enumeration type, refer to MSDN: http://msdn.microsoft.com/zh-cn/library/system.reflection.bindingflags (v = VS.80). aspx
Note: In all the methods that return a Member set, there is an overloaded version that does not get any real parameters. If the BindingFlags parameter is not passed, all these methods return public members. The default value isBindingFlags. Public | BindingFlags. Instance | BindingFlags. Static. (If Public or NonPublic is specified, the Instance must be specified at the same time; otherwise, no members are returned ).
This article is from the "LearningHard" blog, please be sure to keep this source http://learninghard.blog.51cto.com/6146675/1034784