C # System.Reflection (reflection)

Source: Internet
Author: User

In use. NET, the metadata (metadata) and code are stored in the "self-contained" unit, which is called the assembly. We can access this information while the program is running.

In System.Reflection there is a class ———— Assembly that we can use to load a fitting. Here's how:
Assembly Assm=assembly.loadfrom (fileName);
where filename is the file name (with path) of the assembly to be loaded.
Next, we can get the information in the assembly by using the info classes provided in the System.Reflection. First let's take a look at these info classes:
MethodInfo gets the information for a member function and provides access to this member function metadata.
ParameterInfo gets information about a "parameter" and provides access to this "parameter" metadata.
ConstructorInfo gets the information for a "constructor" and provides access to this "constructor" metadata.
PropertyInfo gets information about a property and provides access to this property metadata.
FieldInfo gets the information for a "data member" and provides access to the data member metadata.
EventInfo gets information about an "event" and provides access to this "event" metadata.
The access operations listed above for these classes (except ParameterInfo) are done through a type object. For example, if we want to get a "member function" for a fitting, we need to do this:
System.Reflection.Assembly Ass=system.reflection.assembly.loadfrom (fileName);
Type[] Tp=ass. GetTypes ();
System.reflection.methodinfo[] mi=tp[0]. GetMethods ();
We can also get other information using the same method as follows:
Get the "constructor" information: system.reflection.constructorinfo[] ci=tp[0]. GetConstructors ();
Get the "attribute" information: system.reflection.propertyinfo[] pi=tp[0]. GetProperties ();
Get data member information: system.reflection.fieldinfo[] fi=tp[0]. GetFields ();
Get "event" information: system.reflection.eventinfo[] ei=tp[0]. GetEvents ();
In addition, we can obtain the parameter information for the "member function" and "constructor" through the ParameterInfo class, as follows:
Gets the parameter information for the member function: system.reflection.parameterinfo[] pi=mi[0]. GetParameters ();
Gets the parameter information for the constructor: system.reflection.parameterinfo[] pi=ci[0]. GetParameters ();
The ParameterInfo class has two important properties: Name and ParameterType. Through them we can get the name and data type of "parameter".
Because. NET encapsulates class information in the form of "metadata" in a program or component, and provides a series of ways to get "metadata," so we can dynamically access the information during the program's run.

The following excerpt from Baidu: http://zhidao.baidu.com/question/294391411.html
Personal superficial understanding, reflection is actually getting the properties and methods in the assembly.
Implementation steps:
1, import using System.Reflection;
2,assembly.load ("assembly") loads the assembly, and the return type is a Assembly
3, foreach (Type type in assembly. GetTypes ())
{
String t = type. Name;
}
Get the names of all classes in the assembly
4,type Type = assembly. GetType ("assembly. Class name"); Gets the type of the current class
5,activator.createinstance (type); Create this type instance
6,methodinfo mInfo = type. GetMethod ("Method name"); Get Current method
7,minfo.invoke (null, method parameter);

The original: http://ziseliuxingzh.blog.163.com/blog/static/51120863200832832311528/

C # System.Reflection (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.