Table of Contents
Project structure
Define the plug-in interface
Plug-in implementation
Run the program
Home Backend Development C#.Net Tutorial Summarize practical applications of reflection technology.

Summarize practical applications of reflection technology.

Jun 23, 2017 pm 03:00 PM
reflection application plug-in

The use of reflection has been summarized before. This article combines a complete project to summarize the practical application of reflection.

Project structure

As shown below:

Define the plug-in interface

In the project ConsoleApplication6 In .IService, two interfaces are defined. Run represents driving and Trun represents steering. The following code:

 1 namespace ConsoleApplication6.IService 2 { 3     /// <summary> 4     /// 创建一个车的接口 5     /// </summary> 6     public interface ICarService 7     { 8         /// <summary> 9         /// 行驶10         /// </summary>11         void Run();12 13         /// <summary>14         /// 转向15         /// </summary>16         /// <param name="direction"></param>17         void Turn(Direction direction);18 19     }20 21     public enum Direction22     {23         East,24         Weast,25         South,26         North27     }28 }
Copy after login

Plug-in implementation

Two new projects have been created here to implement plug-ins, namely ConsoleApplication6.Service.BMW and ConsoleApplication6.Service.BenZ. The codes are as follows:

 1 namespace ConsoleApplication6.Service.BMW 2 { 3     public class BMWCarService: ICarService 4     { 5         /// <summary> 6         /// 行驶 7         /// </summary> 8         public void Run() 9         {10             Console.WriteLine("BMW Car Run!");11         }12 13         /// <summary>14         /// 转向15         /// </summary>16         /// <param name="direction"></param>17         public void Turn(Direction direction)18         {19             Console.WriteLine(string.Format("BMW Car turn:{0}", direction.ToString()));20         }21     }22 }
Copy after login
 1 namespace ConsoleApplication6.Service.BenZ 2 { 3     public class BenZCarService: ICarService 4     { 5         /// <summary> 6         /// 行驶 7         /// </summary> 8         public void Run() 9         {10             Console.WriteLine("BenZ Car Run!");11         }12 13         /// <summary>14         /// 转向15         /// </summary>16         /// <param name="direction"></param>17         public void Turn(Direction direction)18         {19             Console.WriteLine(string.Format("BenZ Car turn:{0}", direction.ToString()));20         }21     }22 }
Copy after login

Run the program

Next we can use reflection to run the plug-in program, as follows:

 1 namespace ConsoleApplication6 2 { 3     class Program 4     { 5         static void Main(string[] args) 6         { 7             var assembly = Assembly.Load("ConsoleApplication6.Service.BMW");//也可以从配置文件中读取 8             var t = assembly.GetType("ConsoleApplication6.Service.BMW.BMWCarService");//也可以从配置文件中读取 9 10             //创建一辆车的实例11             var obj = Activator.CreateInstance(t);12             ICarService car = obj as BMWCarService;13             if (car != null)14             {15                 car.Run();16                 car.Turn(Direction.East);17             }18 19             Console.ReadKey();20         }21     }22 }
Copy after login

In this way, a simple plug-in program is completed. At the same time, if we develop a similar plug-in framework, reflection technology will be used extensively.

The above is the detailed content of Summarize practical applications of reflection technology.. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Reflection mechanism implementation of interfaces and abstract classes in Java Reflection mechanism implementation of interfaces and abstract classes in Java May 02, 2024 pm 05:18 PM

The reflection mechanism allows programs to obtain and modify class information at runtime. It can be used to implement reflection of interfaces and abstract classes: Interface reflection: obtain the interface reflection object through Class.forName() and access its metadata (name, method and field) . Reflection of abstract classes: Similar to interfaces, you can obtain the reflection object of an abstract class and access its metadata and non-abstract methods. Practical case: The reflection mechanism can be used to implement dynamic proxies, intercepting calls to interface methods at runtime by dynamically creating proxy classes.

How to use reflection to access private fields and methods in golang How to use reflection to access private fields and methods in golang May 03, 2024 pm 12:15 PM

You can use reflection to access private fields and methods in Go language: To access private fields: obtain the reflection value of the value through reflect.ValueOf(), then use FieldByName() to obtain the reflection value of the field, and call the String() method to print the value of the field . Call a private method: also obtain the reflection value of the value through reflect.ValueOf(), then use MethodByName() to obtain the reflection value of the method, and finally call the Call() method to execute the method. Practical case: Modify private field values ​​and call private methods through reflection to achieve object control and unit test coverage.

How to Undo Delete from Home Screen in iPhone How to Undo Delete from Home Screen in iPhone Apr 17, 2024 pm 07:37 PM

Deleted something important from your home screen and trying to get it back? You can put app icons back on the screen in a variety of ways. We have discussed all the methods you can follow and put the app icon back on the home screen. How to Undo Remove from Home Screen in iPhone As we mentioned before, there are several ways to restore this change on iPhone. Method 1 – Replace App Icon in App Library You can place an app icon on your home screen directly from the App Library. Step 1 – Swipe sideways to find all apps in the app library. Step 2 – Find the app icon you deleted earlier. Step 3 – Simply drag the app icon from the main library to the correct location on the home screen. This is the application diagram

The role and practical application of arrow symbols in PHP The role and practical application of arrow symbols in PHP Mar 22, 2024 am 11:30 AM

The role and practical application of arrow symbols in PHP In PHP, the arrow symbol (-&gt;) is usually used to access the properties and methods of objects. Objects are one of the basic concepts of object-oriented programming (OOP) in PHP. In actual development, arrow symbols play an important role in operating objects. This article will introduce the role and practical application of arrow symbols, and provide specific code examples to help readers better understand. 1. The role of the arrow symbol to access the properties of an object. The arrow symbol can be used to access the properties of an object. When we instantiate a pair

Explore the advantages and application scenarios of Go language Explore the advantages and application scenarios of Go language Mar 27, 2024 pm 03:48 PM

The Go language is an open source programming language developed by Google and first released in 2007. It is designed to be a simple, easy-to-learn, efficient, and highly concurrency language, and is favored by more and more developers. This article will explore the advantages of Go language, introduce some application scenarios suitable for Go language, and give specific code examples. Advantages: Strong concurrency: Go language has built-in support for lightweight threads-goroutine, which can easily implement concurrent programming. Goroutin can be started by using the go keyword

How to use reflection to dynamically modify variable values ​​in golang How to use reflection to dynamically modify variable values ​​in golang May 02, 2024 am 11:09 AM

Go language reflection allows you to manipulate variable values ​​at runtime, including modifying Boolean values, integers, floating point numbers, and strings. By getting the Value of a variable, you can call the SetBool, SetInt, SetFloat and SetString methods to modify it. For example, you can parse a JSON string into a structure and then use reflection to modify the values ​​of the structure fields. It should be noted that the reflection operation is slow and unmodifiable fields cannot be modified. When modifying the structure field value, the related fields may not be automatically updated.

Security considerations and best solutions for golang reflection Security considerations and best solutions for golang reflection May 04, 2024 pm 04:48 PM

Reflection provides type checking and modification capabilities in Go, but it has security risks, including arbitrary code execution, type forgery, and data leakage. Best practices include limiting reflective permissions, operations, using whitelists or blacklists, validating input, and using security tools. In practice, reflection can be safely used to inspect type information.

Introduction to Golang reflection and analysis of application scenarios Introduction to Golang reflection and analysis of application scenarios Apr 03, 2024 pm 01:45 PM

The reflection feature in the Go language allows a program to inspect and modify the structure of a type at runtime. By using Type, Value and reflect.Kind, we can obtain the type information, field values ​​and methods of the object, and we can also create and modify objects. Specific operation methods include: checking type (TypeOf()), obtaining field value (ValueOf(), FieldByName()), modifying field value (Set()), and creating object (New()).

See all articles