Home > Article > Backend Development > asp.net reflection simple application example code
This article mainly introduces the simple application of asp.net reflection. It analyzes the principle and simple usage of asp.net reflection in the form of a complete example. Friends in need can refer to it. Next
The example in this article describes the simple application of asp.net reflection. Share it with everyone for your reference, the details are as follows:
Reflection provides objects (Type type) that encapsulate assemblies, modules and types. You can use reflection to dynamically create instances of a type, bind the type to an existing object, or obtain the type from an existing object and call its methods or access its fields and properties. If properties are used in your code, you can use reflection to access them. ----This is the simplest understanding of reflection. The following is the simplest example to describe the application of reflection technology! 1. Declare the interface, which 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 the interface and implement the methods in the interface. 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+"开始跑了!"); } } }
3. Use reflection technology to create an instance of the type and call the instance method. A simple example like the following
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(); } } }
is completed, and the displayed result is "aaa started running". The naming
controlof reflection is System.Reflection. You mustreference this named control when using it. The long-used object of this named control is Assembly, which contains many staticmethod. Among them, Load is very typical. CreateInstance is used to create an instance of an object.
The above is the detailed content of asp.net reflection simple application example code. For more information, please follow other related articles on the PHP Chinese website!