一直想试一下.net反射的强大功能,也一直有看别人的例子,但今天想自己亲自试一下,结果发现了问题。
下面的代码在VS2003里执行就没任何问题。在vs2005(Development Edition)里编译也没问题,但执行时出现了下面的错误:Object does not match target type
出错行在调用 Result = myProperty.GetValue(ctr)时出现
不知是.net 2.0的bug还是我的问题。
代码如下:
哈哈,我还没找到原因,先记下来给大家看看!
下面的代码在VS2003里执行就没任何问题。在vs2005(Development Edition)里编译也没问题,但执行时出现了下面的错误:Object does not match target type
出错行在调用 Result = myProperty.GetValue(ctr)时出现
不知是.net 2.0的bug还是我的问题。
代码如下:
using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
namespace WindowsApplication2
{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
/**//// <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 );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(74, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 16);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(74, 51);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(76, 93);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(76, 137);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "textBox2";
//
// button1
//
this.button1.Location = new System.Drawing.Point(76, 194);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(239, 278);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, EventArgs e)
{
for(int i = 1 ;i<= 2;i++)
{
this.SetValueControlProperty(this, "label" + i.ToString(), "Text",(string)GetValueControlProperty(this, "textBox" + i.ToString(), "Text"));
}
}
public object GetValueControlProperty(object ClassInstance,string ControlName,string PropertyName)
{
object Result=null;
Type myType = ClassInstance.GetType();
FieldInfo myFieldInfo = myType.GetField( ControlName,
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.Instance);
if(myFieldInfo != null)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myType);
PropertyDescriptor myProperty = properties.Find(PropertyName, true);
if(myProperty != null)
{
object ctr;
ctr = myFieldInfo.GetValue(ClassInstance);
try
{
Result = myProperty.GetValue(ctr);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Error");
}
}
}
return Result;
}
//
public object SetValueControlProperty(object ClassInstance ,string ControlName,string PropertyName,object Value)
{
object Result=null;
Type myType = ClassInstance.GetType();
FieldInfo myFieldInfo = myType.GetField(ControlName,
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.Instance);
if(myFieldInfo != null)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myType);
PropertyDescriptor myProperty = properties.Find(PropertyName, false);
if(myProperty != null)
{
object ctr;
ctr = myFieldInfo.GetValue(ClassInstance);
try
{
myProperty.SetValue(ctr, Value);
Result = ctr;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
}
}
return Result;
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Reflection;
namespace WindowsApplication2
{
/**//// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.Button button1;
/**//// <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 );
}
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.button1 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(74, 12);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(104, 16);
this.label1.TabIndex = 0;
this.label1.Text = "label1";
//
// label2
//
this.label2.Location = new System.Drawing.Point(74, 51);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(100, 23);
this.label2.TabIndex = 1;
this.label2.Text = "label2";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(76, 93);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "textBox1";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(76, 137);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 3;
this.textBox2.Text = "textBox2";
//
// button1
//
this.button1.Location = new System.Drawing.Point(76, 194);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 4;
this.button1.Text = "button1";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(239, 278);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.label2);
this.Controls.Add(this.label1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
/**//// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, EventArgs e)
{
for(int i = 1 ;i<= 2;i++)
{
this.SetValueControlProperty(this, "label" + i.ToString(), "Text",(string)GetValueControlProperty(this, "textBox" + i.ToString(), "Text"));
}
}
public object GetValueControlProperty(object ClassInstance,string ControlName,string PropertyName)
{
object Result=null;
Type myType = ClassInstance.GetType();
FieldInfo myFieldInfo = myType.GetField( ControlName,
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.Instance);
if(myFieldInfo != null)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myType);
PropertyDescriptor myProperty = properties.Find(PropertyName, true);
if(myProperty != null)
{
object ctr;
ctr = myFieldInfo.GetValue(ClassInstance);
try
{
Result = myProperty.GetValue(ctr);
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"Error");
}
}
}
return Result;
}
//
public object SetValueControlProperty(object ClassInstance ,string ControlName,string PropertyName,object Value)
{
object Result=null;
Type myType = ClassInstance.GetType();
FieldInfo myFieldInfo = myType.GetField(ControlName,
BindingFlags.NonPublic | BindingFlags.Instance |
BindingFlags.Public | BindingFlags.Instance);
if(myFieldInfo != null)
{
PropertyDescriptorCollection properties = TypeDescriptor.GetProperties(myType);
PropertyDescriptor myProperty = properties.Find(PropertyName, false);
if(myProperty != null)
{
object ctr;
ctr = myFieldInfo.GetValue(ClassInstance);
try
{
myProperty.SetValue(ctr, Value);
Result = ctr;
}
catch(Exception ex)
{
MessageBox.Show(ex.Message,"错误");
}
}
}
return Result;
}
}
}
哈哈,我还没找到原因,先记下来给大家看看!
转载于:https://www.cnblogs.com/RockyWang/archive/2005/12/06/291507.html