Reflection is a very powerful mechanism. It can be used to dynamically generate an object. You can also view the Attribute applied to the type.
Of course, reflection may have some impact on efficiency, but it is not easy to implement or even implement it in many places without reflection. Therefore, we need to look at this problem dialectically.
The following is a very simple code. You can view the type and the member information of the type.
// Assembly-inclusion-> module-inclusion-> type-inclusion-> element
The Code is as follows:
Using System; using System. reflection; namespace zuo_TestReflectionProject {# region "Program entry" public class Program {static void Main () {People pa = new People ("Hong Meng", "Taoist", 3600); Console. writeLine (pa); Console. writeLine (new string ('-', 40); Type tx = pa. getType (); // reflection entry to obtain the Console type. writeLine ("type name: {0} \ n", tx. name); Console. writeLine ("Full name: {0} \ n", tx. fullName); Console. writeLine (new string ('-', 40); object [] what = tx. ge TCustomAttributes (typeof (SoftInfoAttribute), false); // extract the features of this type from foreach (SoftInfoAttribute si in what) {// traverses the Console of the feature item. writeLine (si. infoType); Console. writeLine (si. author); Console. writeLine (si. dm); if (! String. isNullOrEmpty (si. memo) {Console. writeLine (si. memo);} Console. writeLine (new string ('-', 40);} MethodInfo [] mis = tx. getMethods (
BindingFlags. Instance
| BindingFlags. Static
| BindingFlags. Public
| BindingFlags. NonPublic
| BindingFlags. declaredOnly); // list all the methods in the Console. writeLine ("list all of its methods: \ n"); foreach (MethodInfo mi in mis) {Console. writeLine ("method name: {1} {2} {0} ();", mi. name, mi. memberType, mi. returnType );
// Check whether the method has the function of object [] Using Trigger = mi. getCustomAttributes (typeof (SoftInfoAttribute), false); foreach (SoftInfoAttribute ss in each tritri) {// lists the feature items used in the method Console. writeLine ("\ t {0} {1} {2} {3}", ss. infoType, ss. author, ss. dm, ss. memo) ;}} Console. writeLine ("\ n"); Assembly currentAssembly = Assembly. getExecutingAssembly (); // load the Assembly, current Assembly Console. writeLine ("current Assembly name: {0}", currentAssembly. fullName); Console. writeLine ("\ n current assembly Module:"); Module [] MyModules = currentAssembly. getModules (); // obtain the Module foreach (Module me in MyModules) {Console. writeLine ("module name: {0}", me. name);} // assembly-inclusion-> module-inclusion-> type-inclusion-> element Console. writeLine ("\ n current assembly Type: \ n"); Type [] AssTypes = currentAssembly. getTypes (); // obtain all classes foreach (Type sm in AssTypes) {Console. writeLine ("type name: {0}", sm. name); Console. writeLine ("\ n \ t all members:"); // obtain all Class Members MemberInfo [] mems = sm. getMembers (
BindingFlags. Instance
| BindingFlags. Static
| BindingFlags. Public
| BindingFlags. NonPublic
| BindingFlags. declaredOnly); foreach (MemberInfo mem in mems) {Console. writeLine ("\ t {0} {1}", mem. memberType, mem) ;}} Console. writeLine ("\ n instantiate Test class"); object [] parameters = new object [] {"Reflection created Test class object ...... "}; Object priobj = currentAssembly. CreateInstance (" zuo_TestReflectionProject.Test ",
True,
BindingFlags. Default,
Null,
Parameters,
Null,
Null); // call the method Type tt = typeof (Test); tt. invokeMember ("show", BindingFlags. invokeMethod, null, priobj, null ); // call method }}# endregion # region "application area" public class Test {// create a private string tname; public Test (string t) class with a private constructor) {this. tname = t; Console. writeLine ("Who woke me up from my sleep? ");} Public void show () {Console. writeLine (this. tname) ;}} [SoftInfo ("Destroyer", "Ancore", "3000-01-01", Memo = "the last day is approaching, and everything is done! ")] [SoftInfo (" modifier "," Rich "," 2012-01-01 ", Memo =" improved algorithm ")] [SoftInfo (" creator "," Zuo ankun ", "2011-09-27")] public class People {private string firstName; private string lastName; private int age; private string info; public People (): this (String. empty, String. empty, 0) {} public People (string f, string l, int a) {this. firstName = f; this. lastName = l; this. age = a;} public string FirstName {get {return this. firstName ;}} public string LastName {get {return this. lastName ;}} public int Age {get {return this. age ;}} public string Info {get {return this.info;} set {this.info = value ;}} [SoftInfo ("message sending", "communication", "2011-09-27 ", memo = "a period of rapid technological development! ")] Public void SendMessage (People pp, string s) {pp. Info = s; Console. WriteLine (" message sent successfully! ");} Public override string ToString () {return String. format ("Last Name: {0} Name: {1} age: {2}", this. firstName, this. lastName, this. age) ;}# endregion # region "feature area" [AttributeUsage (AttributeTargets. class | AttributeTargets. method, AllowMultiple = true)] public class SoftInfoAttribute: Attribute {private string infoType; // type private string author; // author private DateTime dm; // Date private string memo; // remarks public SoftInfoAttribute (string it, string au, string date) {this. infoType = it; this. author = au; this. dm = Convert. toDateTime (date);} public string InfoType {get {return this. infoType ;}} public string Author {get {return this. author ;}} public DateTime Dm {get {return this. dm ;}} public string Memo {get {return this. memo;} set {this. memo = value ;}}# endregion}