. NET short description reflection (dynamic call)

Source: Internet
Author: User

We continue to learn basic C # knowledge. This article mainly describes the key step for C # programmers to move towards Advanced C # programmers.
Some may say that this is not the case. Can I develop it without reflection? Yes, of course, but it is definitely different to use it. Any complicated abstract layered architecture or complex design patterns are built on these foundations, for example, we need to implement modular and componentized development, strictly eliminate coupling between modules, and call dynamic interfaces. Such a powerful and flexible system development requires reflection. Any technology has the value and significance of its existence. As long as we use it in a proper position, we can exert amazing power; we can minimize the amount of code we write, making our code clearer and concise;
Next we will learn the concept of reflection in C # and how to use it. what benefits does it provide for us? What's more, can we increase our technical level;
1. What is reflection?
To quickly learn a technology, first of all, we need to understand what this technology is. We need to have a general picture and a general understanding, in order to give full play to everyone's instinctive understanding ability, instead of taking you to understand my thoughts, we can learn faster and, without knowing it, we can form our own set of independent thinking abilities; [Wang qingpei has all rights reserved. For more information, please sign it.]
Natural explanation: radiation is a natural phenomenon, represented by the reverse reaction of the stimulus to the stimulus. This is a literal explanation of reflection. Let's take a look at reflection in computer programming;
Programming explanation: using System. class in the Reflection namespace and System. type. You can obtain information about the loaded assembly and types defined in it (such as classes, interfaces, and value types. You can also use reflection to create type instances at runtime and call and access these instances. This is an official Microsoft MSDN definition;
Through the comparison between the two, I think for our beginners, reflection is to reflect another thing through something. When we were young, we liked to use a mirror to look at the wall, there will be small aperture. This is a typical reflection example. We can understand it as a reflection in a computer. It is what I can't directly obtain using a certain object to reflect.
1:

 
I think this picture is still quite vivid. At least we already have this concept in our minds. Reflection and reflection are the same thing that gets to the other end through some intermediate device.
C # the reflection in it is the same as the above concepts, and the principle is the same. Next we will use the sample code to explain it. I want to learn through this article, you can fully understand the concept of reflection.
2. C # reflection example
2:
 
There are two projects in my solution, Reflection is the console application, TestDll is the project we want to reflect, in this project I defined a Math class, that is, the object to be reflected;

For ease of demonstration, we need to set the output path generated by the TestDll project, that is, the startup directory of our main program;
3:


After this setting, the output file of our project will be in the startup directory of our application, and reflection will be a little more convenient. In order to allow beginners to avoid detours, I will pay for it;

Compile by F6 and output the TestDll. dll file to the Debug directory of Reflection. We switch to the Main method in Reflection to start the Reflection operation;
View sourceprint?
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 ");
Type math = dll. GetType ("TestDll. Math", true );
MethodInfo method = math. GetMethod ("add ");
Int count = (int) method. Invoke (null, new object [] {10, 20 });
Console. WriteLine ("Invoke Method:" + count. ToString ());
Console. ReadLine ();
}
}
}
I defined a static method add in the Math class. Here we can get the Math object through reflection for dynamic calling;
The Assembly object is like a mirror in our daily life. I use this mirror to take a look at the external DLL file, and all the data in that file can be reflected to me, I have made a series of get calls through the managed object. The specific object readers can search for it online. I am just getting started with the introduction;
Summary: Is it amazing to use reflection? I feel that reflection is really powerful. This article is just a simple application of reflection. I will explain how to use reflection for High-configuration application system development; in fact, it is not difficult to grasp the syntax of a language. What is difficult is the principle behind the language. Software is a hardware processing process for hardware programming and all the logic of software. Therefore, it is imperative for us to quickly learn to use a language and then learn the real mysteries of software. Thank you.

Related Article

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.