Dynamic loading of the principle - the use of metadata

xiaoxiao2021-03-06  34

When using .NET created, Metadata and Code (CODE) are stored in a "self-contained" unit, which is called an assembly. We can access this information during the program.

There is such a Class ---- Assembly in System.Reflection, we can load a fitting with it. Methods as below:

AskEMBLY ASSM = askMBLY.LOADFROM (FileName);

Where filename is the file name (with path) of the assembly to load.

Next, we can get information in the assembly by using Info Classes provided in System.Reflection. Let us first look at these Info classes:

MethodInfo Gets information of 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 information about a "constructor" and provides access to this "constructor" metadata.

PropertyInfo gets information about a "property" and provides access to this Properties "metadata.

FieldInfo gets information about a "data member" and provides access to this "data member" metadata.

EventInfo Gets an "event" information and provides access to this "event" metadata.

The access operations of these classes (except ParameterInfo) listed above are completed through a TYPE object. For example, we have to get a "member function" of a fitting:

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 as follows:

Get "constructor" information: system.reflection.constructorinfo [] Ci = TP [0] .getconstructors ();

Get the "Properties" information: system.reflection.propertyInfo [] pi = TP [0] .Getproperties ();

Get "data members" 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 of "Member Functions" and "Constructor" through the ParameterInfo class, as follows:

Get the parameter information of "Member Function": system.reflection.ParameterInfo [] Pi = Mi [0] .GetParameters ();

Get the parameter information of "Constructor": system.reflection.ParameterInfo [] Pi = Ci [0] .GetParameters ();

ParameterInfo classes have two important properties: name and parametertype. Through them we can get the name and data type of "parameter".

Since .NET encapsulates Class's information in the form of "metadata" in the program or components, it provides a series of methods that can get "metadata", so we can run access to this information during operation.

转载请注明原文地址:https://www.9cbs.com/read-67374.html

New Post(0)