C #-Reflection

Source: Internet
Author: User

Reflection is a very powerful function, but it seems to be a bit of consumption performance, people use it cautiously.

1. What does reflection do?

With reflection, we can get the original data in the assembly.

1. What is an assembly?

DLL, EXE these will be a lot of code to achieve the specific functionality of the file package (my own understanding, probably wrong!) )。

2. What are the use cases?

Compiler prompt function

Anti-compilation

3. Below is a direct example of the code, for reference.

(1) First build a class library called Common, in which a class called person, the code of the class is as follows.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespacecommon{ Public classPerson {//name        Private string_name;  Public stringName {Get{return_name;} Set{_name =value;} }        //Age        Private int_age;  Public intAge {Get{return_age;} Set{_age =value;} }         //Print name and age         Public voidPrintfnameage () {Console.WriteLine (_name);        Console.WriteLine (_age); }        //Constructors with parameters         PublicPerson (stringNameintAge ) {             This. Name =name;  This. Age =Age ; }    }}
View Code

(2) in the construction of a form program, wherein the Program.cs buy as follows, which implements a number of common methods of reflection.

Before running this program, you will compile the common and then go to debug to copy the Common.dll to the debug you have built the program.

usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Reflection;//Reflection//Author Cui//Date 20160808namespaceReflection {classProgram {Static voidMain (string[] args) {            //1. Loading assembly Files            stringPath = AppDomain.CurrentDomain.BaseDirectory +"Common.dll"; Assembly=assembly.loadfile (path); //2. Three functions to get data from a dataset//2.1 Gets the type of the specified objectType t =. GetType ("Common.person");            Console.WriteLine (T.name); //2.2 Gets the metadata, regardless of the public private. type[] Type1 =The .            GetTypes (); Console.WriteLine ("Print 2.2 Gets the metadata, regardless of the public private. "); foreach(Type Iteminchtype1) {Console.WriteLine (item.                Name); Console.WriteLine (item.                FullName); Console.WriteLine (item.            Namespace); }            //2.3 Value gets the publictype[] Type2 =The .            Getexportedtypes (); Console.WriteLine ("\ n Print 2.3 Values to get the public"); foreach(Type Iteminchtype2) {Console.WriteLine (item.                Name); Console.WriteLine (item.                FullName); Console.WriteLine (item.            Namespace); }            //3 Creating Objects//3.1 Calling the default parameterless constructor in person//Object obj1 =. CreateInstance ("Common.person");//this is usually not necessary if there is a constructor with parameters that cannot be created. //3.2 You can construct an object with a function parameter            ObjectObj2 = Activator.CreateInstance (t/*Object Type*/,"Xiao Ming", -); //3.2.1 Getting an array of properties for a data sourcepropertyinfo[] Strpro =Obj2. GetType ().            GetProperties (); Console.WriteLine ("\ n Print 3.2.1 Get an array of properties for the data source"); foreach(PropertyInfo IteminchStrpro) {Console.WriteLine (item.            Name); }            //3.2.2 An array of methods to get the data sourceMethodinfo[] Methods =Obj2. GetType ().            GetMethods (); Console.WriteLine ("\ n Print 3.2.2 An array of methods to get the data source"); foreach(MethodInfo Iteminchmethods) {Console.WriteLine (item.            Name); }            //3.3 Call function, print. MethodInfo MD = Obj2. GetType (). GetMethod ("Printfnameage"); Console.WriteLine ("\ n Call the Printfnameage method in person to print the name and age"); Md. Invoke (Obj2,NULL);        Console.readkey (); }    }}
View Code

Summarize:

Referring to a lot of people's blogs, it is found that in order to understand the reflection also requires a lot of other knowledge, the use of this content of knowledge, has been simple to use reflection.

Knowledge points must be one to learn, accumulate more, will be connected to a string.

C #-Reflection

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.