C # language series lecture (16) Dynamic type query

xiaoxiao2021-03-06  79

Dynamic Type Query We know that C # compiled PE files mainly consist of IL code and metadata, metadata provides a rich self-description feature, which makes we can know the types in the component at code runtime. Information. In C # This is done by a mechanism to be amended. First look at an example, first create a simple type: // simpleType.cs public class myclass {private int count = 100; public int count {get {return count;} set {count = value;}} public void print () {System.console.writeline (count);}} Compiles the files above the file with the compile command to get SIMPLETYPE.DLL. Next, a test program for query type: //test.cs using system; using system.reflection; class test {public static void main (string [] args) {type t = typeof (myclass); // Get the type of Myclass Information console.writeline ("Type Name: {0}", T.Name); // Get the name of the type FieldInfo [] FIARR = T.GetFields (); // Get all public domain console.write ("THE) {0} Fields: ", Fiarr.Length); Foreach (FieldInfo O IN Fiarr) {Console.Write (oNAME " ");} console.writeline (); propertyInfo [] piarr = t.Getproperties (); / / Get all public attributes console.write ("THE {0} Properties:", Piarr.Length); Foreach (PropertyInfo O IN Piarr) {Console.Write (O.Name ");} console.writeLine (); MIARR = T. GetMethods (); // Get all public methods console.write ("The {0} methods:", Miarr.Length); Foreach (MethodInfo O IN Miarr) {Console.write (O. Name "" ";}}} CSC /R :SIMPLETYPE.DLL TEST.CS is compiled, you can get the following output: The Type name: myclass the 0 Fields: The 1 Properties: Count The 7 Methods: GetHashcode Equals toString get_count set_count print getType In the above example, first get the type information of the MyClass class through TypeOf (MyClass), of course, can also create an object instance, then call the object instance GetType method to get (each class is inherited from the Object root class). After you have type information (variable t), you can get its name, the type of public domain, public property, and public method containing. Note: Here the C # map mechanism only allows the acquisition type public information, which meets the object-oriented package principle.

This is also why the output of the query type is "The 0 Fields:" - If you change the count domain in SimpleType.cs to public public, it will get its query information. All of these methods are all public methods inherited from the Object class, while methods get_count and set_count are "by-products" to implement count attribute - this is in line with the inquiry of the present. Variations. In fact, a wide variety of members of the System.Type makes us access to almost all types of public information related to the type. The individual classes under System.Reflection namespace can obtain more detailed information more detailed, such as the parameters of the method and the return value, the type of the domain, and the respective values ​​of the enumeration. Dynamic creation and call actual release of the type information of the component is far more than the type of information, which can also be dynamically invoke the dynamic invoke of the type of time when the type information is obtained, and even dynamically created and executed IL. Code! Dynamic calls to C # components provide a late-binding function, which makes the components in runtime integration! Take a look at how to complete the dynamic creation and method of completing the dynamic creation and method of the object: // DynamiceXe.cs Using System; use system.reflection; class test {public static void main () {askSEMBLY A = askPLY.LOADFROM ("SimpleType.dll"); // Loading Components Foreach (Type T.gettypes ()) {i (t.isclass&&! t.isabstract) {methodInfo [] miarr = t.getMethods ); // Get the type of public method Object O = Activator.createInstance (T); // Create an instance (Non-never constructor) Foreach (MIII IN Miarr) {if (! Mi.isabstract &&! Mi.isstatic && mi) .GetParameters (). Length == 0) {Object Re = mi.invoke (o, null); // Call instance method console.writeline ("{0}, return: {1}", mi.name, re) After compiling the CSC /R :SIMPLETYPE.DLL DYNAMICEXE.CS, you can get the following output: GetHashcode, Return: 8 TOSTRING, RETURN: MyClass Get_count, Return: 100 100 Print, Return: GetType, Return: MyClass We give the method name and return value that is dynamically called in the above example. The fourth behavior 100 output is 100, which is the output of dynamic calling method myclass.print (). It is to be pointed out that the call is the type of public non-parameter instance method. Give the name of the component, apply Assembly.LoadFrom, we can dynamically load the components. Activator.createInstance allows dynamically created types (here only by unparalleled constructor), actually uses the type created by it and "MyClass O = New MyClass ()" is the same as the type created. Further, it is also possible to dynamically call them on the basis of the query. Microsoft.NETs provide a very solid foundation for mapping mechanisms from the underlying metadata design.

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

New Post(0)