In. Net, the Assembly (Assembly) holds the metadata (MetaData) information, so you can parse the metadata to get the contents of the assembly, such as classes, methods, properties, and so on, which greatly facilitates the dynamic creation of instances at run time. MSDN explains the following: Reflection provides objects (type types) that encapsulate assemblies, modules, and types. You can use reflection to dynamically create an instance of a type, bind the type to an existing object, or get the type from an existing object and call its methods or access its fields and properties. If you use attributes in your code, you can use reflection to access them. Main purpose: Dynamic load DLL, implement plug-in mechanism. Instantiates a type in a DLL. Performs late binding to access methods of types created at run time. Today I will introduce the following two purposes, in fact, the original purpose is to create a corresponding object based on the values in the configuration file. DLL first a piece of code, this is the CLASSGREENERYCN class to invoke, it is compiled to DllDemo.dll. Using System;namespace dlldemo{public class Classgreenerycn {public string Name {get; set;} public bool Istest {get; set;} public void Hello () {Console.WriteLine (Name); The simple code is that there are two properties in a class, and the Hello method prints the name of name to the command line. Use reflection to create a class instance in the DLL and invoke the method now creates a new command-line project for one purpose, dynamically loading DllDemo.dll, and then instantiating an object named Greenerycn,istest to True and calling the Hello method. The detailed steps are as follows: 1. Namespaces referencing reflection: Using SYSTEM.REFLECTION;2. dynamic load DLL dynamically loading DLLs have 3 functions: public static Assembly Load (String assemblystring) ; The method passes in the name of the DLL, which must be in the global cache GAC, or it will be reported as "System.IO.FileLoadException: Failed to load file or assembly" exception. public static Assembly LoadFile (String paTH); This loadfile is the most convenient, parameter is the path of the DLL. public static Assembly LoadFrom (string assemblyfile); This method is also possible, and the parameters are also DLL paths. 3. Gets the type of the Classgreenerycn class var type = asm. GetType ("DLLDEMO.CLASSGREENERYCN"); Note that the full type name, including the signed namespace, is required here. 4. Create an instance of this type var instance = asm. CreateInstance ("DLLDEMO.CLASSGREENERYCN"); 5. Set the property type. GetProperty ("Name"). SetValue (instance, "http://greenerycn.cnblogs.com", null), type. GetProperty ("Istest"). SetValue (instance, true, null); 6. Gets the Hello method var = type. GetMethod ("Hello"); 7. Call the Hello method Method.invoke (instance, null); 8. Compile and run Reflaction_demo complete code using System.Reflection; Namespace reflectiondlldemo{class Program {static void Main (string[] args) {var asm = as sembly. LoadFile (@ "D:\3_code\DotNet\DllDemo\DllDemo\bin\Debug\DllDemo.dll"); var type = asm. GetType ("DLLDEMO.CLASSGREENERYCN"); var instance = asm. CreateInstance ("DLLDEMO.CLASSGREENERYCN"); Type. GetProperty ("Name"). SetValue (instance, "http://greenerycn.cnblogs.com", null); Type. GetProperty ("Istest"). SetValue (instance, true, null); var method = type. GetMethod ("Hello"); Method. Invoke (instance, null); } }}
Reflection in C #