C # sharp experience the 16th lecture

xiaoxiao2021-03-06  75

C # sharp experience Nanjing University of Posts and Telecommunications Li Jianzhong (Lijianzhong@263.net.cn) The sixteenth lecture Dynamic Type Query We know that the C # compiled PE file is mainly composed of IL code and metadata, and the metadata is provided. Net component Rich self-description characteristics, which makes we can know important information in the type of components at the time of runtime. In C # This is done by a mechanism called a REFLECTION. Let's first look at an example, we first create a simple type: // simpleType.cspublic class myclass {private int count = 100; public int count {get {return count;} set {count = value;}} public void print ) {System.console.writeline (count);}} Compile the above file with the compile command CSC / T: library simpleType.cs to get the SIMPLETYPE.DLL output. Let's take a query type test program: //test.csusing system; using system.reflection; class test {public static void

Main

(String [] args) {type t = typeof (myclass); // Get the type information of MyClass console.writeline ("Type Name: {0}", T.Name); // Get the name of the name FieldInfo [] Fiarr = T.Getfields (); // Get all common 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 (.name "");} console.writeline (); methodInfo [] miarr = t.getMethods (); // Get all public methods console.write ("the {0} methods: ", miarr.length); Foreach (MethodInfo O IN Miarr) {console.write (.name ");}}} After compiling CSC /R :SIMPLETYPE.DLL TEST.CS, execution can get the following Output: The Type Name: Myclassthe 0 Fields: The 1 Properties: Count the 7 Methods: GetHashcode Equals Tostring Get_Count Set_count Print Gett YPE In the above example, we first get the type information of type myclass through TypeOf (MyClass), of course, we can also get the object instance, then call the GetType method of the object instance (inheritance from the Object root class. Obtain this method). After you have type information (variable t), we can get its type name, which contains public domains, public properties, and public methods. Note that the mapping mechanism of the C # only allows us to get the type of public information, which conforms to the principle of object-oriented package. This is why we have the output of the query type, which is "The 0 Fields:" - If we change the count field in SimpleType.cs to public public, we will get his query information. All of these methods are public methods inherited from the Object class, while the method get_count and set_count is our "by-product" we implement the count property - this is in line with the attributes of our previously described. Variations. In fact, the SYSTEM.TYPE class makes us access to almost all types of public information related to types.

Each class under System.Reflection namespace is more detailed information, such as the parameters of the method, the return value, the type of the domain, the respective values ​​of the enumeration. Dynamic creation and calling actual map is far more than dynamically known to the type information of the component, it can also make us dynamically create and modify the dynamic call of the type of time when the type information is obtained, even Dynamically create and execute IL code! Dynamic calls to C # components provide a night binding feature, which makes it extremely convenient to integrate between the components! Use the simple components of the previously created SimpleType.dll, let's take a look at how to complete the dynamic creation and method of the object: // DynamiceXe.csusing system; use system.reflection; class test {public static voidmain

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

New Post(0)