Learn about type information at .NET runtime (1)

zhaozj2021-02-17  44

By reflecting the class and System.Type in the namespace, you can get information about the loaded assembly and the types (such as classes, interfaces, and value types) defined. You can also use reflections to create a type instance at runtime, then call and access these instances.

Reflection overview

Public Language Runture Loader Management Application Domain. Such management includes loading each assembly to the corresponding application domain and controlling the memory layout of each program centralized type hierarchy.

The assembly contains modules, and the module contains types, and the type is also included. Reflection provides an object of package assembly, modules, and types. You can create a type of instance using a reflection, bind the type to an existing object, or get the type from an existing object. Then, you can call the type method or access its fields and properties. Reflection usually has the following uses:

Using Assembly Definitions and Load the assembly, load the modules listed in the assembly list, and find the type of the type and create an instance of this type.

Using Module Understand the following similar information: contains the assembly of the module and class in the module. You can also get all global methods or other specific non-global methods defined on the module.

Using CONSTRUctorInfo to learn the following similar information: constructor's name, parameters, access modifiers (such as public or private) and implementation of details (such as Abstract or Virtual). Use the getConstructors or getConstructor method of the Type object to call a specific constructor.

Using MethodInfo to learn about the following information: the name, return type, parameter, access modifier (such as public or private) and implementation details (such as Abstract or Virtual). Use the Type object's getMethods or getMethod method to call a specific method.

Using FieldInfo to learn about the following information: the name of the field, access the modifier (such as public or private) and implementing details (such as static); and gets or sets a field value.

Use EventInfo to learn about the following similar information: the name of the event, the event handler data type, custom property, the declaration type, and the type of reflectance, etc .; and add or remove the event handler.

Use PropertyInfo to learn about the following similar information: the name of the property, the data type, the declaration type, the reflection type, and only read or can be writable, and the property value is obtained or set.

Use ParameterInfo to learn about the following information: the name of the parameter, data type, parameter is the input parameter or output parameters, and the location of the parameters in the method signature.

System.Reflection.emit Namespace class provides a special form of reflections that allow you to construct a type at runtime.

Reflection can also be used to create an application called type browser that allows users to select type and then view information about selected types.

There are other uses in the reflection. Language compilers such as JScript use reflection to construct symbol tables. System.Runtime.Serialization The class in the namespace uses the reflection to access the data and determine the field to be held. The class in the System.Runtime.Remoting namespace is indirectly used by serialization.

View Type Information

The System.Type class has a core role in reflecting. When the type of reflection request is loaded, the public language runtime will create a TYPE object for it. You can find all information about this type using the methods, fields, properties, and nested classes of the Type object.

When using Assembly.gettype or Assembly.gettype, you can get the name of the desired type when you use Assembly.gettype, and you can get the TYPE object from the unloaded program. Use Type.getType to get the TYPE object from the loaded program. Use module.gettype and module.gettype to get the module TYPE object. The following code example displays the syntax necessary to get the assembly assembly object and module.

[C #]

// Get the mscorlib assembly in which the object is defined.

AskEMBLY A = TypeOf (Object) .Module.Assembly;

The following example code shows how to get a TYPE object from the loaded program.

[C #]

// load an assembly use ITS file name.

AskEMBLY A = askMBLY.LOADFROM ("myexe.exe");

// Get the Type name from the assembly.

TYPE [] TYPES2 = a.gettypes ();

Foreach (Type T in Types2)

{

Console.writeline (T.Fullname);

}

After getting a TYPE object, you can learn about the information about this type of member through a variety of ways. For example, by calling the Type.getMembers method (the method will get a group of MemberInfo objects described for each member of the current type), you can get information about all members of this type.

You can also use methods on the Type class to retrieve information about one or more constructor, methods, events, fields, or attributes specified by name. For example, Type.getConstructor encapsulates a specific constructor of the current class.

If you have a TYPE object, you can use the type.module property to get an object of the module in which this type is located. Use the module.assembly property to find the objects of the package of the package module. Use the type.assembly property to get the package of the package type.

System.Type and ConstructorInfo

The following code example displays how to list a constructor of a class (String class in this example).

[C #]

// this Program lists all the public constructors

// of the system.string class.

Using system;

Using system.reflection;

Class Listmembers {

Public static void main (String [] args) {

TYPE T = TypeOf (System.String);

Console.Writeline ("Listing All the public constructors of the {0} type", t);

// conntructors

Constructorinfo [] Ci = T.Getconstructors (BindingFlags.Public | BindingFlags.instance);

Console.writeline ("// Constructors");

PrintMembers (CI);

}

Public static void printmembers (MemberInfo [] ms) {

Moreach (MemberInfo M IN MS) {

Console.writeLine ("{0} {1}", "", m);

}

Console.writeline ();

}

MemberInfo, MethodInfo, FieldInfo and PropertyInfo

Use MemberInfo, MethodInfo, FieldInfo or PropertyInfo objects to get information about types, properties, events, fields.

The following code example uses MemberInfo to list the number of members in the System.IO.File class and use the System.Type.ISPUBLIC property to determine the visibility of the class.

[C #]

Using system;

Using system.io;

Using system.reflection;

Class mymemberinfo

{

Public static void main (string [] args)

{

Console.writeline ("/NREFLECTION.MEMBERINFO");

// Get the Type and MemberInfo.

TYPE mytype = type.gettype ("system.io.file");

Memberinfo [] mymemberinfoarray = mytype.getmembers ();

// Get and Display the declaringtype method.

Console.writeline ("/ Nthere Are {0} MEMBERS IN {1}.",

Mymemberinfoarray.length, mytype.fullname;

Console.writeline ("{0}.", Mytype.fullname;

IF (mytype.ispublic)

{

Console.writeLine ("{0} is public.", Mytype.fullname;

}

}

}

The following code example surveys the type of the specified member. It performs a reflection for a member of the MemberInfo class, then lists it.

[C #]

// this code displays information about the getValue method of fieldInfo.

Using system;

Using system.reflection;

Class mymethodinfo {

Public static int main () {

Console.writeline ("Reflection.MethodInfo");

// Get and Display the Type.

Type mytype = type.gettype ("system.reflection.fieldInfo);

// specify the member for which you want type information here.

MethodInfo mymethodinfo = mytype.getMethod ("getValue");

Console.writeline (Mytype.Fullname "." Mymethodinfo.name);

// Get and Display the MemberType Property.

MEMBERTYPES mymembertypes = mymethodinfo.membertype;

IF (MEMBERTYPES.CONSTRUCTOR == mymembertypes) {

Console.writeline ("MEMBERTYPE IS OF TYPE ALL");

}

Else if (membertypes.custom == mymembertypes) {

Console.WriteLine ("MEMBERTYPE IS OF TYPE CUSTOM");

}

Else if (MemberTypes.Event == mymembertypes) {

Console.writeline ("MEMBERTYPE IS OF TYPE EVENT");

}

Else if (MEMBERTYPES.FIELD == mymembertypes) {

Console.writeline ("MEMBERTYPE IS OF TYPE FIELD");

}

Else if (MEMBERTYPES.METHOD == mymembertypes) {

Console.WriteLine ("MEMBERTYPE IS OF TYPE METHOD");

}

Else if (Membertypes.property == mymembertypes) {

Console.writeline ("MEMBERTYPE IS OF TYPE PROPERTY);

}

Else if (MemberTypes.TypeInfo == mymembertypes) {

Console.writeline ("Membertype IS of Type TypeInfo);

}

Return 0;

}

}

The following sample code uses all reflected * INFO classes and bindingFlags to list all members of the specified class (constructor, field, attributes, events, and methods), and divide these members into static and instance categories.

[C #]

// this program lists all the members of the

// system.io.bufferedstream class.

Using system;

Using system.io;

Using system.reflection;

Class Listmembers {

Public static void main (String [] args) {

// Specify the class here.

TYPE T = TypeOf (System.Io.BufferedStream);

Console.writeLine ("Public and Non Public) of the {0} Type", T);

// static fields are listed first.

FieldInfo [] FI = T.Getfields (BindingFlags.Static |

Bindingflags.nonpublic | bindingflags.public;

Console.writeLine ("// static fields");

PrintMembers (fi);

// static property

PropertyInfo [] PI = T.GetProperties (BindingFlags.Static |

Bindingflags.nonpublic | bindingflags.public;

Console.writeline ("// static property");

PrintMembers (PI);

// static events

EventInfo [] Ei = T.GETEVENTS (BindingFlags.Static |

Bindingflags.nonpublic | bindingflags.public;

Console.writeline ("// static events"); PrintMembers (EI);

// Static Methods

MethodInfo [] MI = T.getMethods (BindingFlags.Static |

Bindingflags.nonpublic | bindingflags.public;

Console.writeline ("// static methods");

PrintMembers (mi);

// conntructors

Constructorinfo [] CI = T.Getconstructors (BindingFlags.instance |

Bindingflags.nonpublic | bindingflags.public;

Console.writeline ("// Constructors");

PrintMembers (CI);

// Instance Fields

Fi = T.Getfields (BindingFlags.instance | BindingFlags.nonpublic |

Bindingflags.public);

Console.writeline ("// instance fields");

PrintMembers (fi);

// Instance Properties

PI = T.GetProperties | BindingFlags.nonpublic |

Bindingflags.public);

Console.writeline ("// instance proferties);

PrintMembers (PI);

// Instance Events

Ei = T.GETEVENTS (BindingFlags.instance | BindingFlags.nonpublic |

Bindingflags.public);

Console.writeline ("// instance events");

PrintMembers (EI);

// Instance Methods

Mi = T.getMethods (bindingflags.instance | bindingflags.nonpublic |

Bindingflags.public);

Console.writeline ("// instance methods");

PrintMembers (mi);

Console.writeline ("/ R / NPRESS RETURN TO EXIT.");

Console.read ();

}

Public static void printmembers (MemberInfo [] ms) {

Moreach (MemberInfo M IN MS) {

Console.writeLine ("{0} {1}", "", m);

}

Console.writeLine ();

}

}

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

New Post(0)