A Brief Introduction to. NET-the only way to break into the threshold of Advanced Programming

Source: Internet
Author: User

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:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Text;
  4.  
  5. Namespace MainInterface
  6. {
  7. /// <Summary>
  8. /// Delegate after successful Computation
  9. /// </Summary>
  10. /// <Param name = "count"> return the calculated value </param>
  11. Public delegate void AddHandler (int count );
  12. /// <Summary>
  13. /// Add method Interface
  14. /// </Summary>
  15. Public interface AddInterface
  16. {
  17.  
  18. /// <Summary>
  19. /// Events after calculation
  20. /// </Summary>
  21. Event AddHandler AddEndEvent;
  22. /// <Summary>
  23. /// Calculate the sum of two numbers
  24. /// </Summary>
  25. /// <Param name = "I"> number1 </param>
  26. /// <Param name = "j"> number2 </param>
  27. Void add (int I, int j );
  28. }
  29. }

Implementation interface:

 
 
  1. Using System;
  2. Using System. Collections. Generic;
  3. Using System. Text;
  4.  
  5. Namespace TestDll
  6. {
  7. Public class Math: MainInterface. AddInterface
  8. {
  9.  
  10. # Region AddInterface Member
  11.  
  12. Public event MainInterface. AddHandler AddEndEvent;
  13.  
  14. Public void add (int I, int j)
  15. {
  16. AddEndEvent (I + j );
  17. }
  18.  
  19. # Endregion
  20. }
  21. }

Specific call:

 
 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using System.Reflection;  
  5. using System.Diagnostics;  
  6.  
  7. namespace Reflection  
  8. {  
  9.     class Program  
  10.     {  
  11.         static void Main(string[] args)  
  12.         {  
  13.             Assembly dll = Assembly.LoadFile(Environment.CurrentDirectory + "\\TestDll.dll");  
  14.  
  15.             foreach (Type type in dll.GetTypes())  
  16.             {  
  17.                 if (type.GetInterface("MainInterface.AddInterface") != null)  
  18.                 {  
  19.                     MainInterface.AddInterface add = Activator.CreateInstance(type, true) as MainInterface.AddInterface;  
  20.                     add.AddEndEvent += new MainInterface.AddHandler(add_AddEndEvent);  
  21.                     add.add(10, 20);  
  22.  
  23.  
  24.                 }  
  25.             }  
  26.  
  27.         }  
  28.  
  29.         static void add_AddEndEvent(int count)  
  30.         {  
  31.  
  32.             Console.WriteLine("Invoke Method:\n" + count.ToString());  
  33.  
  34.             Console.ReadLine();  
  35.         }  
  36.     }  
  37. }  

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.