Asp.net reflection simple application instance and asp.net application instance

Source: Internet
Author: User

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.

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.