We continue to learn the fundamentals of C #, and this article focuses on the critical step of our C # programmers moving toward Advanced C # programmers.
Some friends will say that this is not the case, I do not have to reflect on the development of it? Of course, but with no certainty is not the same, any complex abstract layered architecture or complex design patterns are based on these, for example, we want to be modular, component development, to strictly eliminate the coupling between the modules, to make dynamic interface calls. Such a powerful and flexible system development, must use reflection to do, any technology has its existence of value and significance, as long as we use it in the right place to play an amazing force, as much as possible to reduce the code we write, but also our code will be clear and concise;
Here we will learn the concept of reflection in C # and how to use it, with what it can provide us with what kind of benefits, more importantly, we can improve the technical steps of a height;
1. What do you mean by reflection
To learn a technology quickly, first of all we have to understand what this technology is doing, we need to have a bigger picture, need to have a general understanding, in order to play a person's instinctive understanding of the ability, rather than pull you to understand my thoughts, only so that we can learn faster, Unconsciously we can form their own set of independent thinking ability; [King Qingyue, reproduced please give a signature]
Natural interpretation: The shot is a natural phenomenon, manifested by the stimulus to the stimulus of the inverse reaction; this is the literal interpretation of reflection, we look at the reflection in computer programming;
Programmatic interpretation: by System.Reflection classes in the namespace and System.Type, you can get information about the assemblies that have been loaded and the types that are defined in them, such as classes, interfaces, and value types. You can also use reflection to create type instances at run time, and to invoke and access these instances. This is the official MSDN definition from Microsoft;
By comparing the two, I think that for us beginners, reflection is the reflection of another thing by something; when we were young, we used to look at the wall with a mirror and a small aperture appeared, which is a typical reflection example, We interpret it as a reflection in a computer where I use a certain object to reflect what I can't get directly; [King Qingyue, reproduced please give attribution]
1:
This picture I think is still relatively image, at least in our mind already has such a concept, reflection, reflection is through some intermediary equipment to get to the other end of the thing.
C # Inside the reflection is the same as the above concept, the principle is the same, the following we use code examples to explain, I think through this article to learn, you will be able to thoroughly understand the concept of reflection.
2.c# Reflection Example
2:
In my solution there are two projects, reflection is the console application, Testdll is the project we want to reflect, in this project I define a math class, which is the object we want to reflect;
For demonstration convenience, we need to set up the output path of the Testdll project, that is, the startup directory of our main program;
3:
After this setting, our project's output file will be in our application's startup directory, reflection will be convenient, in order to allow beginners to less detours, I have to add a bit of things;
Press F6 compile, will output TestDll.dll file to reflection Debug directory, we switch to reflection in the main method to start the reflection operation;
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 have defined a static method add in the math class, where we can get the math object to be dynamically called by reflection;
Assembly object is like a mirror in our daily life, I use this mirror to take a look at the external DLL file, the file inside of all the data can be reflected to me, I am in the managed object to make a series of call, the specific object readers can search the Internet, Daniel explained more than I, I'm just doing an introductory introduction;
Summary: Is not with the reflection is very magical, I feel the reflection is really very powerful, this article is just a reflection of a simple application, I will explain the use of reflection for high-configuration application system development; in fact, to master the grammar of a language is not difficult, difficult is the principle behind the language. Software is the hardware programming, all the logic of the software is the processing process of the hardware; so it is imperative to quickly learn to use a language, and then to learn the real software mystery; Thank you.
Reference Address: http://www.cnblogs.com/wangiqngpei557/archive/2011/07/08/2100994.html
. NET Simple talk of reflection (dynamic invocation)