Write an example of a simple C # reflection (with source code)

xiaoxiao2021-03-06  54

Using system.drawing; using system.collections; using system.windows.form; using system.data; using system.reflection;

Summary description of Namespace WindowsApplication4 {///

/// Form1. /// public class Form1: System.Windows.Forms.Form {private FileInfo fileInfo = null; private System.Windows.Forms.Button button1; private System.Windows.Forms.OpenFileDialog OpenFileDialog; private System.Windows. Forms.TextBox textBox1; private System.Windows.Forms.Button button2; private System.Windows.Forms.Button button3; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows. Forms.Label label3; private System.Windows.Forms.RichTextBox rtbMethods; private System.Windows.Forms.RichTextBox rtbPro; private System.Windows.Forms.RichTextBox rtbEvents; private System.Windows.Forms.Button button4; /// // Required designer variable. /// private system.componentmodel.container components = null;

Public Form1 () {// // Windows Form Designer Support for // InitializeComponent ();

// // Todo: Add any constructor code after INITIALIZEComponent call //}

///

/// Clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {components.dispose ();}} Base.Dispose

#REGION Windows Form Designer The code ///

/// designer supports the required method - do not use the code editor to modify the // / this method.

/// private void InitializeComponent () {this.button1 = new System.Windows.Forms.Button (); this.OpenFileDialog = new System.Windows.Forms.OpenFileDialog (); this.textBox1 = new System. Windows.Forms.TextBox (); this.button2 = new System.Windows.Forms.Button (); this.button3 = new System.Windows.Forms.Button (); this.rtbMethods = new System.Windows.Forms.RichTextBox (); ThisLabel1 = new system.windows.forms.label (); this.label2 = new system.windows.forms.label (); this.rtbpro = new system.windows.forms.richtextbox (); this. Label3 = new system.windows.forms.label (); this.rtbevents = new system.windows.forms.richtextbox (); this.button4 = new system.windows.Forms.Button (); this.suspendlayout (); / / / / // button1 // this.button1.location = new system.drawing.point (336, 8); this.button1.name = "button1"; this.button1.size = new system.drawing.size (64, 24 ); This.button1.tabindex = 0; this.button1.text = "brownse"; this.button1.click = new syste M.EventHandler (this.button1_click); // // textBox1 // this.textBox1.Location = new system.drawing.point (8, 8); this.textbox1.name = "textbox1"; this.textbox1.size = New system.drawing.size (320, 21); this.textbox1.tabindex = 1; this.textBox1.text = "textbox1"; // // button2 // this.button2.location = new system.drawing.point ( 624, 8); this.button2.name = "button2"; this.button2.size = new system.drawing.size (136, 24); this.button2.tabindex = 2; this.button2.text = "invokedesmethods" ; // // Button3 // this.button3.location =

New system.drawing.point (16, 312); this.button3.name = "button3"; this.button3.size = new system.drawing.size (144, 24); this.button3.tabindex = 3; this. Button3.text = "invoke"; this.button3.click = new system.eventhandler (this.button3_click); // // rtbmethods // this.rtbmethods.location = new system.drawing.point (8, 56); THIS.RTBMETHODS.NAME = "RTBMETHODS"; this.rtbmethods.size = new system.drawing.size (744, 72); this.rtbmethods.tabindex = 4; this.rtbmethods.text = ""; // Label1 // this.label1.Location = new system.drawing.point (8, 40); this.label1.name = "label1"; this.label1.size = new system.drawing.size (96, 16); this. Label1.tabindex = 5; this.label1.text = "methods"; // // label2 // this.label2.location = new system.drawing.point (8, 128); this.Label2.name = "label2" "This.label2.size = new system.drawing.size (96, 16); this.label2.tabindex = 7; this.label2.text =" property "; // // rtbpro // this.rtbpro.lo CATION = new system.drawing.point (12, 144); this.rtbPro.name = "RTBPRO"; this.rtbPro.size = new system.drawing.size (744, 72); this.rtbPro.tabindex = 6; THIS.RTBPRO.TEXT = ""; /// Label3 // this.label3.Location = new system.drawing.point (16, 216); this.label3.name = "label3"; this.Label3.size = New system.drawing.size (96, 16); this.label3.tabindex = 9; this.label3.text = "events"; /// rtbevents // this.rtbevents.location = new system.drawing.point ( 16, 232);

THIS.RTBEVENTS.NAME = "RTBEVENTS"; this.rtbevents.size = new system.drawing.size (744, 72); this.rtbevents.tabindex = 8; this.rtbevents.text = ""; // // Button4 // this.button4.location = new system.drawing.point (168, 312); this.button4.name = "button4"; this.button4.size = new system.drawing.size (144, 24); this. Button4.tabindex = 10; this.button4.text = "clear"; // // Form1 // this.AutoscaleBasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size 768, 358); this.controls.add (this.button4); this.controls.add (this.label3); this.controls.add (this.rtbevents); this.controls.add (this.label2); this.label2); this.label2 .Controls.add (this.rtbPro); this.controls.add (this.label1); this.controls.add (this.RTBMethods); this.controls.add (this.button3); this.controls.add (this .Button2); this.Controls.add (this.textBox1); this.controls.add (this.button1); this.name = "form1"; this.text = "form1"; this.ResumeLayout (false);} #ndregio n

///

/// The primary entry point of the application. /// [stathread] static void main () {Application.run (new form1 ());}

private void button1_Click (object sender, System.EventArgs e) {if (OpenFileDialog.ShowDialog () == DialogResult.OK) {this.fileInfo = new FileInfo (OpenFileDialog.FileName); this.textBox1.Text = this.fileInfo.FullName }}

private void button3_Click (object sender, System.EventArgs e) {// get the file information Assembly dll = Assembly.LoadFrom (OpenFileDialog.FileName); foreach (Type type in dll.GetTypes ()) {// methods foreach (MethodInfo objMethod in TYPE.GETMETHODS ()) {this.rtbmethods.text = type.name ":" ObjMethod.Name.tostring () "/ r / n";} foreach (propertyInfo objpropertyInfo in type.getproperties ()) {this. RTBMETHODS.TEXT = type.name ":" ObjPropertyInfo.Name.toTString () "/ r / n";} foreach (Eventinfo ObjectInfo in type.getevents ()) {this.rtbevents.text = type.name ": " Objeventinfo.name.toTString () " / r / n ";}}}}}

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

New Post(0)