C # reflection (reflection), feature (atrriction)

Source: Internet
Author: User
Reflection Definition

Reflection is the ability to review metadata and collect information about its type.

What is metadata? Metadata is something in a collection. we know that a program will generate a lot of DLL after compilation, which is an assembly. Generally, a project corresponds to an assembly. an assembly is a DLL or EXE file (but it is said that there are multiple file assembly, I don't know if one assembly is multiple DLL, I have never studied it ). of course, not all DLL files are called assembly. other programming languages, such as C ++, also have DLL. We generally call it a dynamic link library. not an assembly. in addition to executable commands, the collection also contains metadata. metadata is a lot of tables, including class definition tables, field definition tables, and method definition tables. there are also versions and other information. it is a bit like a directory, telling us something in the Assembly.

Most of the time, we do not need to know the metadata information in the Assembly, that is, reflection is not required. When do we need reflection?

Under normal circumstances, if we want to use a DLL, add the reference directly and then reference the namespace. then you can use the class. if you have already agreed on some classes and functions in a DLL. but the DLL is not ready yet. you need to use it again when writing code. you can't wait for others to finish writing code. in addition, if the DLL is not intended to be written for the time being, it will be more so if it is expanded later. this will allow reflection. you can first write the code to load a DLL (not necessarily actually exists), then obtain information about the class and function, and then call it. this is used to call other DLL, and reflection can also be used to call the DLL. For example, the software usually has a "about" menu option, and the version information of the software will pop up. you can obtain version information from the dataset without having to obtain the Registry or any other information. in addition, when we use vs, intelligent sensing makes it very convenient for us to use it. Just knock a few letters to bring out a pile of information. reflection is also used here.

Feature Definition

Atrribute is sometimes translated into attributes, but it is easy to be confused with properties. atrribute refers to additional information that can be applied to code blocks (such as classes, methods, and attributes.

For example, we will use the [serializable] feature when using serialization. to serialize a class, we must add this feature before the class. the usage of the feature is a bracket number, and then add some information.

In addition to the features defined by the class library, we can also customize some features. The feature information will be stored in the Assembly and can be used for reflection.

Reflection usage

For example, if there is a DLL file mydll. DLL, which has a class yourname, which has a method showyourname (). now I want to dynamically load the DLL, instantiate the class yourname through reflection, and call the method in the class.

The definition of Class yourname in DLL is as follows:

Using system;

Namespace mydll

{

Class yourname

{

Public void showyourname ()

{

Console. writeline ("Hi, I am Arwen .");

}

}

}

The called code is as follows. First reference the namespace using system. reflection;

String dllpath = @ "D: \ mydll. dll"; // path of the DLL file

Assembly ASM = assembly. loadfrom (dllpath); // dynamically load DLL

Type yourname = ASM. getType ("mydll. yourname ");/* obtain the yourname type. type can represent the class, interface, and Other types obtained by reflection. A bit like an object can represent any class */

Object OBJ = activator. createinstance (yourname); // instantiate class yourname

Methodinfo function = yourname. getmethod ("showyourname"); // obtain the showyourname method.

Function. Invoke (OBJ, null); // call the showyourname method.

Custom feature usage

A simple example is to create a new feature for adding comments. In fact, a custom feature is to create a class

Class myatrribute: attribute // must inherit the atrribute, and the others are exactly the same as general classes.

{

Public String note {Get; set ;}

Public myatrribute (string MSG)

{

Note = MSG;

}

Public void shownote ()

{

Console. writeline (note );

}

}

Add

[Myatrrid ("this class is used for showing your name")]

Class yourname

{

// Use yourname

}

Use custom feature information in reflection

String dllpath = @ "D: \ mydll. dll ";

Assembly ASM = assembly. loadfrom (dllpath );

Type yourname = ASM. GetType ("mydll. yourname ");

Attribute [] Notes = attribute. getcustomattributes (yourname );

Foreach (VaR at in notes)

{

Myatrribute Myat = at as myatrri;

Console. writeline (Myat. note );

}

We can easily find that there is a problem here, that is, the reference of mydll must be added. Otherwise, where can we find the class myatrribute.

So what should I do if I load mydll dynamically? It will be troublesome.

You have.

String dllpath = @ "D: \ mydll. dll ";

Assembly ASM = assembly. loadfrom (dllpath );

Type myatrritypes = ASM. GetType ("mydll. myatrritype ");

Methodinfo myfunction = myatrrimethod. getmethod ("shownote"); // you must first obtain the function in the feature class.

Type yourname = ASM. GetType ("mydll. yourname ");

Attribute [] Notes = attribute. getcustomattributes (yourname );

Foreach (VAR note in notes)

{

Object myobject = (object) note;

Myfunction. Invoke (myobject, null); // you can obtain the content in the feature.

}

This is troublesome to use. I don't know if there are other simple methods.

 

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.