asp.net reflection simple application example code

怪我咯
Release: 2017-04-01 11:09:36
Original
1229 people have browsed it

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);
  }
}
Copy after login

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+"开始跑了!");
    }
  }
}
Copy after login

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

is completed, and the displayed result is "aaa started running". The naming

control

of 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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!