与java一样,c#也提供了反射的机制,只不过JAVA里面使用Class来表示类的信息,而C#里面使用的是Type来表示类的元数据的存储,本人编写了 一个图形界面的利用c#反射机制得到类的信息的程序,本程序有一点问题,希望大侠能够给予帮助。
程序的运行界面如图:
本程序的源代码如下:
using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
namespace GuiReflect
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtB_Name;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtB_Method;
private System.Windows.Forms.TextBox txtB_property;
private System.Windows.Forms.TextBox txtB_parent;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label labNameSpace;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null ;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager( typeof (Form1));
this .txtB_Name = new System.Windows.Forms.TextBox();
this .label1 = new System.Windows.Forms.Label();
this .groupBox1 = new System.Windows.Forms.GroupBox();
this .labNameSpace = new System.Windows.Forms.Label();
this .label5 = new System.Windows.Forms.Label();
this .label4 = new System.Windows.Forms.Label();
this .label3 = new System.Windows.Forms.Label();
this .label2 = new System.Windows.Forms.Label();
this .txtB_parent = new System.Windows.Forms.TextBox();
this .txtB_property = new System.Windows.Forms.TextBox();
this .txtB_Method = new System.Windows.Forms.TextBox();
this .button1 = new System.Windows.Forms.Button();
this .groupBox1.SuspendLayout();
this .SuspendLayout();
//
// txtB_Name
//
this .txtB_Name.Location = new System.Drawing.Point( 144 , 30 );
this .txtB_Name.Name = " txtB_Name " ;
this .txtB_Name.Size = new System.Drawing.Size( 272 , 21 );
this .txtB_Name.TabIndex = 0 ;
this .txtB_Name.Text = "" ;
//
// label1
//
this .label1.Location = new System.Drawing.Point( 40 , 32 );
this .label1.Name = " label1 " ;
this .label1.Size = new System.Drawing.Size( 56 , 16 );
this .label1.TabIndex = 1 ;
this .label1.Text = " c#类名 " ;
this .label1.Click += new System.EventHandler( this .label1_Click);
//
// groupBox1
//
this .groupBox1.Controls.Add( this .labNameSpace);
this .groupBox1.Controls.Add( this .label5);
this .groupBox1.Controls.Add( this .label4);
this .groupBox1.Controls.Add( this .label3);
this .groupBox1.Controls.Add( this .label2);
this .groupBox1.Controls.Add( this .txtB_parent);
this .groupBox1.Controls.Add( this .txtB_property);
this .groupBox1.Controls.Add( this .txtB_Method);
this .groupBox1.Location = new System.Drawing.Point( 16 , 96 );
this .groupBox1.Name = " groupBox1 " ;
this .groupBox1.Size = new System.Drawing.Size( 624 , 360 );
this .groupBox1.TabIndex = 2 ;
this .groupBox1.TabStop = false ;
this .groupBox1.Text = " 结果显示 " ;
//
// labNameSpace
//
this .labNameSpace.Location = new System.Drawing.Point( 72 , 24 );
this .labNameSpace.Name = " labNameSpace " ;
this .labNameSpace.Size = new System.Drawing.Size( 272 , 23 );
this .labNameSpace.TabIndex = 7 ;
this .labNameSpace.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label5
//
this .label5.Location = new System.Drawing.Point( 16 , 24 );
this .label5.Name = " label5 " ;
this .label5.Size = new System.Drawing.Size( 56 , 23 );
this .label5.TabIndex = 6 ;
this .label5.Text = " 命名空间 " ;
this .label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label4
//
this .label4.Location = new System.Drawing.Point( 462 , 64 );
this .label4.Name = " label4 " ;
this .label4.TabIndex = 5 ;
this .label4.Text = " 父类列表 " ;
this .label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this .label3.Location = new System.Drawing.Point( 264 , 64 );
this .label3.Name = " label3 " ;
this .label3.TabIndex = 4 ;
this .label3.Text = " 属性列表 " ;
this .label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this .label2.Location = new System.Drawing.Point( 24 , 64 );
this .label2.Name = " label2 " ;
this .label2.Size = new System.Drawing.Size( 176 , 23 );
this .label2.TabIndex = 3 ;
this .label2.Text = " 方法列表 " ;
this .label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// txtB_parent
//
this .txtB_parent.Location = new System.Drawing.Point( 416 , 96 );
this .txtB_parent.Multiline = true ;
this .txtB_parent.Name = " txtB_parent " ;
this .txtB_parent.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this .txtB_parent.Size = new System.Drawing.Size( 192 , 240 );
this .txtB_parent.TabIndex = 2 ;
this .txtB_parent.Text = "" ;
//
// txtB_property
//
this .txtB_property.Location = new System.Drawing.Point( 226 , 96 );
this .txtB_property.Multiline = true ;
this .txtB_property.Name = " txtB_property " ;
this .txtB_property.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this .txtB_property.Size = new System.Drawing.Size( 176 , 240 );
this .txtB_property.TabIndex = 1 ;
this .txtB_property.Text = "" ;
//
// txtB_Method
//
this .txtB_Method.Location = new System.Drawing.Point( 24 , 96 );
this .txtB_Method.Multiline = true ;
this .txtB_Method.Name = " txtB_Method " ;
this .txtB_Method.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this .txtB_Method.Size = new System.Drawing.Size( 176 , 240 );
this .txtB_Method.TabIndex = 0 ;
this .txtB_Method.Text = "" ;
//
// button1
//
this .button1.Location = new System.Drawing.Point( 520 , 29 );
this .button1.Name = " button1 " ;
this .button1.TabIndex = 3 ;
this .button1.Text = " 获得列表 " ;
this .button1.Click += new System.EventHandler( this .button1_Click);
//
// Form1
//
this .AutoScaleBaseSize = new System.Drawing.Size( 6 , 14 );
this .ClientSize = new System.Drawing.Size( 656 , 469 );
this .Controls.Add( this .button1);
this .Controls.Add( this .groupBox1);
this .Controls.Add( this .label1);
this .Controls.Add( this .txtB_Name);
this .FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this .Icon = ((System.Drawing.Icon)(resources.GetObject( " $this.Icon " )));
this .MaximizeBox = false ;
this .Name = " Form1 " ;
this .Text = " 代码反射机制的应用 " ;
this .groupBox1.ResumeLayout( false );
this .ResumeLayout( false );
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run( new Form1());
}
private void label1_Click( object sender, System.EventArgs e)
{
}
private void button1_Click( object sender, System.EventArgs e)
{
labNameSpace.Text = "" ;
txtB_Method.Text = "" ;
txtB_property.Text = "" ;
txtB_parent.Text = "" ;
if (txtB_Name.Text == "" )
txtB_Method.Text = " 类名不能为空 " ;
else
{
string test = " System. " + txtB_Name.Text.ToString().Trim();
try
{
Type carType = Type.GetType(test);
labNameSpace.Text = carType.Namespace.ToString();
// 得到类的方法代码
MethodInfo [] methods = carType.GetMethods();
int NumMethod = 0 ;
foreach (MethodInfo method in methods)
{
NumMethod ++ ;
txtB_Method.Text += NumMethod + " -> " + method.Name.ToString() + " " ;
}
// 得到类的属性的代码
PropertyInfo [] properties = carType.GetProperties();
int NumPropertie = 0 ;
foreach (PropertyInfo propertie in properties)
{
NumPropertie ++ ;
txtB_property.Text += NumPropertie + " -> " + propertie.Name.ToString() + " " ;
}
// 得到类的层次机构的代码
int NumParent = 0 ;
Type parent = carType.BaseType;
while (parent != null )
{
NumParent ++ ;
txtB_parent.Text += NumParent + " -> " + parent.Name.ToString() + " " ;
parent = parent.BaseType;
}
}
catch (TypeLoadException e1)
{
txtB_Method.Text = e1.Message;
}
catch (Exception e2)
{
txtB_Method.Text = e2.Message;
}
}
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
namespace GuiReflect
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.TextBox txtB_Name;
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.TextBox txtB_Method;
private System.Windows.Forms.TextBox txtB_property;
private System.Windows.Forms.TextBox txtB_parent;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label labNameSpace;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null ;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if ( disposing )
{
if (components != null )
{
components.Dispose();
}
}
base .Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new System.Resources.ResourceManager( typeof (Form1));
this .txtB_Name = new System.Windows.Forms.TextBox();
this .label1 = new System.Windows.Forms.Label();
this .groupBox1 = new System.Windows.Forms.GroupBox();
this .labNameSpace = new System.Windows.Forms.Label();
this .label5 = new System.Windows.Forms.Label();
this .label4 = new System.Windows.Forms.Label();
this .label3 = new System.Windows.Forms.Label();
this .label2 = new System.Windows.Forms.Label();
this .txtB_parent = new System.Windows.Forms.TextBox();
this .txtB_property = new System.Windows.Forms.TextBox();
this .txtB_Method = new System.Windows.Forms.TextBox();
this .button1 = new System.Windows.Forms.Button();
this .groupBox1.SuspendLayout();
this .SuspendLayout();
//
// txtB_Name
//
this .txtB_Name.Location = new System.Drawing.Point( 144 , 30 );
this .txtB_Name.Name = " txtB_Name " ;
this .txtB_Name.Size = new System.Drawing.Size( 272 , 21 );
this .txtB_Name.TabIndex = 0 ;
this .txtB_Name.Text = "" ;
//
// label1
//
this .label1.Location = new System.Drawing.Point( 40 , 32 );
this .label1.Name = " label1 " ;
this .label1.Size = new System.Drawing.Size( 56 , 16 );
this .label1.TabIndex = 1 ;
this .label1.Text = " c#类名 " ;
this .label1.Click += new System.EventHandler( this .label1_Click);
//
// groupBox1
//
this .groupBox1.Controls.Add( this .labNameSpace);
this .groupBox1.Controls.Add( this .label5);
this .groupBox1.Controls.Add( this .label4);
this .groupBox1.Controls.Add( this .label3);
this .groupBox1.Controls.Add( this .label2);
this .groupBox1.Controls.Add( this .txtB_parent);
this .groupBox1.Controls.Add( this .txtB_property);
this .groupBox1.Controls.Add( this .txtB_Method);
this .groupBox1.Location = new System.Drawing.Point( 16 , 96 );
this .groupBox1.Name = " groupBox1 " ;
this .groupBox1.Size = new System.Drawing.Size( 624 , 360 );
this .groupBox1.TabIndex = 2 ;
this .groupBox1.TabStop = false ;
this .groupBox1.Text = " 结果显示 " ;
//
// labNameSpace
//
this .labNameSpace.Location = new System.Drawing.Point( 72 , 24 );
this .labNameSpace.Name = " labNameSpace " ;
this .labNameSpace.Size = new System.Drawing.Size( 272 , 23 );
this .labNameSpace.TabIndex = 7 ;
this .labNameSpace.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
//
// label5
//
this .label5.Location = new System.Drawing.Point( 16 , 24 );
this .label5.Name = " label5 " ;
this .label5.Size = new System.Drawing.Size( 56 , 23 );
this .label5.TabIndex = 6 ;
this .label5.Text = " 命名空间 " ;
this .label5.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label4
//
this .label4.Location = new System.Drawing.Point( 462 , 64 );
this .label4.Name = " label4 " ;
this .label4.TabIndex = 5 ;
this .label4.Text = " 父类列表 " ;
this .label4.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label3
//
this .label3.Location = new System.Drawing.Point( 264 , 64 );
this .label3.Name = " label3 " ;
this .label3.TabIndex = 4 ;
this .label3.Text = " 属性列表 " ;
this .label3.TextAlign = System.Drawing.ContentAlignment.MiddleCenter;
//
// label2
//
this .label2.Location = new System.Drawing.Point( 24 , 64 );
this .label2.Name = " label2 " ;
this .label2.Size = new System.Drawing.Size( 176 , 23 );
this .label2.TabIndex = 3 ;
this .label2.Text = " 方法列表 " ;
this .label2.TextAlign = System.Drawing.ContentAlignment.TopCenter;
//
// txtB_parent
//
this .txtB_parent.Location = new System.Drawing.Point( 416 , 96 );
this .txtB_parent.Multiline = true ;
this .txtB_parent.Name = " txtB_parent " ;
this .txtB_parent.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this .txtB_parent.Size = new System.Drawing.Size( 192 , 240 );
this .txtB_parent.TabIndex = 2 ;
this .txtB_parent.Text = "" ;
//
// txtB_property
//
this .txtB_property.Location = new System.Drawing.Point( 226 , 96 );
this .txtB_property.Multiline = true ;
this .txtB_property.Name = " txtB_property " ;
this .txtB_property.ScrollBars = System.Windows.Forms.ScrollBars.Both;
this .txtB_property.Size = new System.Drawing.Size( 176 , 240 );
this .txtB_property.TabIndex = 1 ;
this .txtB_property.Text = "" ;
//
// txtB_Method
//
this .txtB_Method.Location = new System.Drawing.Point( 24 , 96 );
this .txtB_Method.Multiline = true ;
this .txtB_Method.Name = " txtB_Method " ;
this .txtB_Method.ScrollBars = System.Windows.Forms.ScrollBars.Vertical;
this .txtB_Method.Size = new System.Drawing.Size( 176 , 240 );
this .txtB_Method.TabIndex = 0 ;
this .txtB_Method.Text = "" ;
//
// button1
//
this .button1.Location = new System.Drawing.Point( 520 , 29 );
this .button1.Name = " button1 " ;
this .button1.TabIndex = 3 ;
this .button1.Text = " 获得列表 " ;
this .button1.Click += new System.EventHandler( this .button1_Click);
//
// Form1
//
this .AutoScaleBaseSize = new System.Drawing.Size( 6 , 14 );
this .ClientSize = new System.Drawing.Size( 656 , 469 );
this .Controls.Add( this .button1);
this .Controls.Add( this .groupBox1);
this .Controls.Add( this .label1);
this .Controls.Add( this .txtB_Name);
this .FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedSingle;
this .Icon = ((System.Drawing.Icon)(resources.GetObject( " $this.Icon " )));
this .MaximizeBox = false ;
this .Name = " Form1 " ;
this .Text = " 代码反射机制的应用 " ;
this .groupBox1.ResumeLayout( false );
this .ResumeLayout( false );
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run( new Form1());
}
private void label1_Click( object sender, System.EventArgs e)
{
}
private void button1_Click( object sender, System.EventArgs e)
{
labNameSpace.Text = "" ;
txtB_Method.Text = "" ;
txtB_property.Text = "" ;
txtB_parent.Text = "" ;
if (txtB_Name.Text == "" )
txtB_Method.Text = " 类名不能为空 " ;
else
{
string test = " System. " + txtB_Name.Text.ToString().Trim();
try
{
Type carType = Type.GetType(test);
labNameSpace.Text = carType.Namespace.ToString();
// 得到类的方法代码
MethodInfo [] methods = carType.GetMethods();
int NumMethod = 0 ;
foreach (MethodInfo method in methods)
{
NumMethod ++ ;
txtB_Method.Text += NumMethod + " -> " + method.Name.ToString() + " " ;
}
// 得到类的属性的代码
PropertyInfo [] properties = carType.GetProperties();
int NumPropertie = 0 ;
foreach (PropertyInfo propertie in properties)
{
NumPropertie ++ ;
txtB_property.Text += NumPropertie + " -> " + propertie.Name.ToString() + " " ;
}
// 得到类的层次机构的代码
int NumParent = 0 ;
Type parent = carType.BaseType;
while (parent != null )
{
NumParent ++ ;
txtB_parent.Text += NumParent + " -> " + parent.Name.ToString() + " " ;
parent = parent.BaseType;
}
}
catch (TypeLoadException e1)
{
txtB_Method.Text = e1.Message;
}
catch (Exception e2)
{
txtB_Method.Text = e2.Message;
}
}
}
}
}
其中有问题的代码如下:
string test="System."+txtB_Name.Text.ToString().Trim();
Type carType=Type.GetType(test);
labNameSpace.Text=carType.Namespace.ToString();
当我输入Button的时候,会出现“未将对象引用设置到对象的实例”的异常。原因是Type carType=Type.GetType(test)这一句出现了异常,没有得到Button的类型,我想是Button的全名不是System.Button造成的,可是我有暂时找不到更好的办法,希望大侠给予指点。使本程序得到完善。