reflection

xiaoxiao2021-03-06  45

Reflection is usually used in the following four tasks:

1, browse metadata

// Use reflection

Public Class Tester

{

Public static void main ()

{

Mymath mm = new mymath ();

Console.writeline ("Calling Dofunc1 (7). Result: {0}", MM.DOFUNC1 (7));

System.reflection.memberinfo inf = typeof (mymath); // Type class is the main way of accessing metadata Object [] attributes; attributes = inf.getcustomattributes (typeof (bugfixattribute), true); // Incoming the properties to query foreach type of information (Object attribute in attributes) {BugFixAttribute bfa = (BugFixAttribute) attribute; Console.WriteLine ( "/ nBugID: {0}", bfa.BugID); Console.WriteLine ( "Programmer: {0}", bfa .Programmer; "Date: {0}", bfa.date); console.writeline ("Comment: {0}", bfa.comment);} console.readline ();}}} 2, Query Type A. Reflections A Accessories Namespace Programming_csharp {Using System; Using System.Reflection;

Public class test {public static void main () {ask ("mscorlib.dll"); type [] types = A.GETTYPES (); // Type represents Type Declaration: Class, Interface, Array, Value and enumeration Foreach (Type Type in type) {console.writeline ("type is {0}", type);} console.writeline ("{0} type content", types.length; console.readline () ;}}} B. Reflection a type of public class test {public static void main () {type atype = type.gettype ("system.reflection.assembly"); // extracts a type console.writeLine with a gettype () method "/ nsingle type is {0} / n", THETYPE); console.readline ();}} c. Reflection a type member public class test tester {public static void main () {type thetype = type.gettype ("system .Reflection.Assembly "); // extract a type console.writeline with a getType () method (" / nsingle type is {0} / n ", theType);

MemberInfo [] mbrinfoarray = THYPE.GETMEMBERS (); // list all methods, fields, properties foreach (memberinfo mbrinfo in mbrinfoarray) {Console.Writeline ("{0} is a {1}", mbrinfo, mbrinfo.membertype) Console.readline ();}} d. Search type method Sometimes we only pay attention to methods, do not need fields, nature, etc. Public class tester {public static void main () {type theType = type.gettype ("system.reflection.assembly"); // extract a type console.writeline with a getType () method ("/ nsingle type is {0} / n ", theType); // MemberInfo [] mbrInfoArray = theType.GetMembers (BindingFlags.LookupAll); // list all the methods, fields, properties MemberInfo [] mbrInfoArray = theType.GetMethods (); foreach (MemberInfo mbrInfo in mbrInfoArray) {Console.writeline ("{0} is a {1}", mbrinfo, mbrinfo.membertype);} console.readline ();}} e. Find a specific member public class tester {public static void main () {Type Thetpe = Type.GetType ( "System.Reflection.Assembly"); MemberInfo [] mbrInfoArray = theType.FindMembers (MemberTypes.Method, BindingFlags.Public | BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.DeclaredOnly, Type.FilterName , "Get *"); foreach (MemberInfo Mbrinfo In Mbrinfoarray) {Console.WriteLine ("{0} is a {1}", mbrinfo, mbrinfo.membertype);} console.readline ();}} 3, // Dynamic calling a method public class tester {Public static void Main () {Type theMathType = Type.GetType ( "System.Math"); // Since System.Math has no public constructor, this // would throw an exception // Object theObj = Activator.CreateInstance (. ThemathType);

Type [] paramtype = new type [1]; paramtype [0] = type.gettype ("system.double");

MethodInfo CosineInfo = ThemathType.getMethod ("COS", paramtype); object [] parameters = new object [1]; parameters [0] = 45 * (Math.pi / 180); // Object retvalue = cosineinfo.invoke (theobj , parameters; Object retvalue = cosineinfo.invoke (themathtype, parameters);

Console.writeline ("The Cosine of a 45 Degree Angle {0}", RetValue; console.readline ();

}} 4, send reflection // code from "C # programming"

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

New Post(0)