We continue to learn C # basic knowledge. This article plays an important role in the previous friends who learned basic knowledge. In order to continue the enthusiasm for basic knowledge learning, I wrote this special article.
The focus of this article is to borrow ". NET short talk reflection (dynamic call) "continues to play, so that friends can get in one breath, in the end, where reflection can be used, and how advanced it can be.
Next I will explain the specific examples. If you don't talk nonsense, please come with me;
1: Basic knowledge required
C # interface: to use reflection for advanced use, you must first have the interface basics. Only when you use interfaces can the system truly survive. Refer to the. NET short interface article;
C # Delegate and event: In the process of dynamic calling, we will inevitably need to transfer some data. Such transfer is to use interfaces for transmission, and we cannot be strongly coupled. Refer to the. NET short article on events and delegation;
C # reflection: This is the most critical point. Everything is centered around this point. In our example below, we need to dynamically use reflection to obtain interface objects. Refer to the. NET short introduction to reflection (dynamic call;
2. Example content
The main content of the example is to apply the basic knowledge we have learned to consolidate our foundation so that we can use it flexibly in real projects, raise yourself to a new height;
We all know that interfaces are standards, we all know event delegation, and we all know reflection, but we only know these scattered knowledge points. How can we put these small technologies together to form a solid code structure;
The general content of the example is as follows: we define an interface and use an object to implement it. When we use it, we dynamically call it through reflection, however, during reflection, I need to use interfaces to determine the uniqueness, because we do not know who has implemented the interface, so the benefits of the interface come out;
3: start learning Samples
Interface Definition:
- Using System;
- Using System. Collections. Generic;
- Using System. Text;
-
- Namespace MainInterface
- {
- /// <Summary>
- /// Delegate after successful Computation
- /// </Summary>
- /// <Param name = "count"> return the calculated value </param>
- Public delegate void AddHandler (int count );
- /// <Summary>
- /// Add method Interface
- /// </Summary>
- Public interface AddInterface
- {
-
- /// <Summary>
- /// Events after calculation
- /// </Summary>
- Event AddHandler AddEndEvent;
- /// <Summary>
- /// Calculate the sum of two numbers
- /// </Summary>
- /// <Param name = "I"> number1 </param>
- /// <Param name = "j"> number2 </param>
- Void add (int I, int j );
- }
- }
Implementation interface:
- Using System;
- Using System. Collections. Generic;
- Using System. Text;
-
- Namespace TestDll
- {
- Public class Math: MainInterface. AddInterface
- {
-
- # Region AddInterface Member
-
- Public event MainInterface. AddHandler AddEndEvent;
-
- Public void add (int I, int j)
- {
- AddEndEvent (I + j );
- }
-
- # Endregion
- }
- }
Specific call:
- using System;
- using System.Collections.Generic;
- using System.Text;
- using System.Reflection;
- using System.Diagnostics;
-
- namespace Reflection
- {
- class Program
- {
- static void Main(string[] args)
- {
- Assembly dll = Assembly.LoadFile(Environment.CurrentDirectory + "\\TestDll.dll");
-
- foreach (Type type in dll.GetTypes())
- {
- if (type.GetInterface("MainInterface.AddInterface") != null)
- {
- MainInterface.AddInterface add = Activator.CreateInstance(type, true) as MainInterface.AddInterface;
- add.AddEndEvent += new MainInterface.AddHandler(add_AddEndEvent);
- add.add(10, 20);
-
-
- }
- }
-
- }
-
- static void add_AddEndEvent(int count)
- {
-
- Console.WriteLine("Invoke Method:\n" + count.ToString());
-
- Console.ReadLine();
- }
- }
- }
Conclusion: although the sparrow is small and dirty, the sample code above has no effect, but it can basically summarize what we use in daily life. This is the only way for us to enter the framework development and system development;
This article is from the pattern driven the world blog, please be sure to keep this source http://wangqingpei557.blog.51cto.com/1009349/606631