Asp.net reflection simple application instance and asp.net application instance
This article describes the simple application of asp.net reflection. We will share this with you for your reference. The details are as follows:
Reflection provides encapsulated assembly, module, and Type objects (Type ). You can use reflection to dynamically create instances of the type, bind the type to an existing object, or obtain the type from the existing object and call its method or access its fields and properties. If attributes are used in the code, you can use reflection to access them.---- This is the simplest way to understand reflection. The following is a simple example to describe the application of reflection technology!
1. Declare an interface that contains a virtual method. As follows:
using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace ConsoleApplication1{ public interface IReflect { void Run(string name); }}
2. implement interfaces to implement methods in interfaces. As follows:
Using System; using System. collections. generic; using System. linq; using System. text; namespace ConsoleApplication1 {public class Reflect: IReflect {public void Run (string name) {Console. writeLine (name + "started to run! ");}}}
3. Use reflection technology to create and call the methods of instances. As follows:
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Reflection;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { IReflect rec = (IReflect)Assembly.Load("ConsoleApplication1").CreateInstance("ConsoleApplication1.Reflect"); rec.Run("aaa"); Console.ReadLine(); } }}
The result of such a simple instance is "aaa started to run ". The reflected naming control is System. Reflection. It must be referenced when used. The long-used object of the naming control is Assembly, and the object contains many static methods. Load is typical. CreateInstance is an instance used to create an object.