Home  >  Article  >  Backend Development  >  Summarize practical applications of reflection technology.

Summarize practical applications of reflection technology.

零下一度
零下一度Original
2017-06-23 15:00:351448browse

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 }

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 }
 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 }

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 }

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:
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
Previous article:static classNext article:static class