ASP. NET reflection Simple Application Instance code

Source: Internet
Author: User
This article mainly introduces the simple application of ASP, combined with a complete example to analyze the principle of ASP and simple use of the method, the need for friends can refer to the following

This example describes the simple application of ASP. Share to everyone for your reference, as follows:

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. ----This is the simplest understanding of reflection. Here is one of the simplest examples to tell about the application of reflection technology!

One. Declares 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);}  }

Two. 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+ "started running!");  }}}

Three. Create an instance of the type by using reflection technology and invoke the method of the instance. 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 ();}}}  

Such a simple example is done, and the result is "AAA started running." The named control that is reflected is system.reflection, and you must refer to the named control when you use it, the object that the named control long uses is assembly, which contains many static methods. where load is typical. CreateInstance is used to create an instance of an object.

Related Article

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.