English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

Simple Application Example of ASP.NET Reflection

This article describes a simple application of asp.net reflection. Share it with everyone for reference, as follows:

Reflection provides objects that encapsulate assemblies, modules, and types (Type type). You can use reflection to dynamically create instances of types, bind types to existing objects, or obtain types from existing objects and call their methods or access their fields and properties. If properties are used in the code, reflection can be used to access them.----This is the simplest understanding of reflection. Below is a simple example to demonstrate the application of reflection technology!

One. Declare an interface that contains a virtual method. For example

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. For example

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+"Start running!");
    }
  }
}

Three. Create instances of types using reflection technology and call methods of instances. For example

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();
    }
  }
}

This simple example is completed, and the displayed result is 'aaa started running'. The reflection naming control is System.Reflection. It is necessary to refer to this naming control when using it. The commonly used object of this naming control is Assembly, which contains many static methods. Load is a very typical one. CreateInstance is used to create an instance of an object.

Readers who are interested in more about asp.net can check the special topics on this site: 'Summary of asp.net Optimization Techniques', 'Collection of asp.net String Operation Techniques', 'Summary of asp.net XML Operation Techniques', 'Summary of asp.net File Operation Techniques', 'Special Topic of asp.net AJAX Techniques', and 'Summary of asp.net Caching Operation Techniques'.

I hope the content described in this article will be helpful to everyone in asp.net program design.

Statement: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#oldtoolbag.com (Please replace # with @ when sending an email to report violations, and provide relevant evidence. Once verified, this site will immediately delete the content suspected of infringement.)

You May Also Like