关于TypeDescriptor反射取得对象属性的例外,是.net 2.0的bug吗?

    一直想试一下.net反射的强大功能,也一直有看别人的例子,但今天想自己亲自试一下,结果发现了问题。
下面的代码在VS2003里执行就没任何问题。在vs2005(Development Edition)里编译也没问题,但执行时出现了下面的错误:Object does not match target type
出错行在调用      Result = myProperty.GetValue(ctr)时出现

%E6%9C%AA%E5%91%BD%E5%90%8D.GIF
不知是.net 2.0的bug还是我的问题。
    代码如下:
None.gif using  System;
None.gif
using  System.Drawing;
None.gif
using  System.Collections;
None.gif
using  System.ComponentModel;
None.gif
using  System.Windows.Forms;
None.gif
using  System.Data;
None.gif
None.gif
using  System.Reflection;
None.gif
None.gif
namespace  WindowsApplication2
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
ExpandedSubBlockStart.gifContractedSubBlock.gif    
/**//// <summary>
InBlock.gif    
/// Form1 的摘要说明。
ExpandedSubBlockEnd.gif    
/// </summary>

InBlock.gif    public class Form1 : System.Windows.Forms.Form
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
private System.Windows.Forms.Label label1;
InBlock.gif        
private System.Windows.Forms.Label label2;
InBlock.gif        
private System.Windows.Forms.TextBox textBox1;
InBlock.gif        
private System.Windows.Forms.TextBox textBox2;
InBlock.gif        
private System.Windows.Forms.Button button1;
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 必需的设计器变量。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private System.ComponentModel.Container components = null;
InBlock.gif
InBlock.gif        
public Form1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
//
InBlock.gif            
// Windows 窗体设计器支持所必需的
InBlock.gif            
//
InBlock.gif
            InitializeComponent();
InBlock.gif
InBlock.gif            
//
InBlock.gif            
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
InBlock.gif            
//
ExpandedSubBlockEnd.gif
        }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 清理所有正在使用的资源。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        protected override void Dispose( bool disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if( disposing )
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
if (components != null
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    components.Dispose();
ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
base.Dispose( disposing );
ExpandedSubBlockEnd.gif        }

InBlock.gif
ContractedSubBlock.gifExpandedSubBlockStart.gif        
Windows 窗体设计器生成的代码#region Windows 窗体设计器生成的代码
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
InBlock.gif        
/// 此方法的内容。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        private void InitializeComponent()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this.label1 = new System.Windows.Forms.Label();
InBlock.gif            
this.label2 = new System.Windows.Forms.Label();
InBlock.gif            
this.textBox1 = new System.Windows.Forms.TextBox();
InBlock.gif            
this.textBox2 = new System.Windows.Forms.TextBox();
InBlock.gif            
this.button1 = new System.Windows.Forms.Button();
InBlock.gif            
this.SuspendLayout();
InBlock.gif            
// 
InBlock.gif            
// label1
InBlock.gif            
// 
InBlock.gif
            this.label1.Location = new System.Drawing.Point(7412);
InBlock.gif            
this.label1.Name = "label1";
InBlock.gif            
this.label1.Size = new System.Drawing.Size(10416);
InBlock.gif            
this.label1.TabIndex = 0;
InBlock.gif            
this.label1.Text = "label1";
InBlock.gif            
// 
InBlock.gif            
// label2
InBlock.gif            
// 
InBlock.gif
            this.label2.Location = new System.Drawing.Point(7451);
InBlock.gif            
this.label2.Name = "label2";
InBlock.gif            
this.label2.Size = new System.Drawing.Size(10023);
InBlock.gif            
this.label2.TabIndex = 1;
InBlock.gif            
this.label2.Text = "label2";
InBlock.gif            
// 
InBlock.gif            
// textBox1
InBlock.gif            
// 
InBlock.gif
            this.textBox1.Location = new System.Drawing.Point(7693);
InBlock.gif            
this.textBox1.Name = "textBox1";
InBlock.gif            
this.textBox1.Size = new System.Drawing.Size(10021);
InBlock.gif            
this.textBox1.TabIndex = 2;
InBlock.gif            
this.textBox1.Text = "textBox1";
InBlock.gif            
// 
InBlock.gif            
// textBox2
InBlock.gif            
// 
InBlock.gif
            this.textBox2.Location = new System.Drawing.Point(76137);
InBlock.gif            
this.textBox2.Name = "textBox2";
InBlock.gif            
this.textBox2.Size = new System.Drawing.Size(10021);
InBlock.gif            
this.textBox2.TabIndex = 3;
InBlock.gif            
this.textBox2.Text = "textBox2";
InBlock.gif            
// 
InBlock.gif            
// button1
InBlock.gif            
// 
InBlock.gif
            this.button1.Location = new System.Drawing.Point(76194);
InBlock.gif            
this.button1.Name = "button1";
InBlock.gif            
this.button1.Size = new System.Drawing.Size(7523);
InBlock.gif            
this.button1.TabIndex = 4;
InBlock.gif            
this.button1.Text = "button1";
InBlock.gif            
this.button1.Click += new System.EventHandler(this.button1_Click);
InBlock.gif            
// 
InBlock.gif            
// Form1
InBlock.gif            
// 
InBlock.gif
            this.AutoScaleBaseSize = new System.Drawing.Size(614);
InBlock.gif            
this.ClientSize = new System.Drawing.Size(239278);
InBlock.gif            
this.Controls.Add(this.button1);
InBlock.gif            
this.Controls.Add(this.textBox2);
InBlock.gif            
this.Controls.Add(this.textBox1);
InBlock.gif            
this.Controls.Add(this.label2);
InBlock.gif            
this.Controls.Add(this.label1);
InBlock.gif            
this.Name = "Form1";
InBlock.gif            
this.Text = "Form1";
InBlock.gif            
this.ResumeLayout(false);
InBlock.gif            
this.PerformLayout();
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif        
#endregion

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
InBlock.gif        
/// 应用程序的主入口点。
ExpandedSubBlockEnd.gif        
/// </summary>

InBlock.gif        [STAThread]
InBlock.gif        
static void Main() 
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            Application.Run(
new Form1());
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif
InBlock.gif        
private void button1_Click(object sender, EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
for(int i = 1 ;i<= 2;i++)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif
InBlock.gif                
this.SetValueControlProperty(this"label" + i.ToString(), "Text",(string)GetValueControlProperty(this"textBox" + i.ToString(), "Text"));
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
public object GetValueControlProperty(object ClassInstance,string ControlName,string PropertyName)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
object Result=null;
InBlock.gif            Type myType 
= ClassInstance.GetType();
InBlock.gif            FieldInfo myFieldInfo 
= myType.GetField( ControlName,
InBlock.gif                BindingFlags.NonPublic 
| BindingFlags.Instance  |
InBlock.gif                BindingFlags.Public    
| BindingFlags.Instance);
InBlock.gif            
if(myFieldInfo != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PropertyDescriptorCollection properties 
= TypeDescriptor.GetProperties(myType);
InBlock.gif                PropertyDescriptor myProperty 
= properties.Find(PropertyName, true);
InBlock.gif
InBlock.gif                
if(myProperty != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
object ctr;
InBlock.gif
InBlock.gif                    ctr 
= myFieldInfo.GetValue(ClassInstance);
InBlock.gif
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        Result 
= myProperty.GetValue(ctr);
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MessageBox.Show(ex.Message,
"Error");
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return Result;
ExpandedSubBlockEnd.gif        }

InBlock.gif    
InBlock.gif        
//
InBlock.gif
        public object SetValueControlProperty(object ClassInstance ,string ControlName,string PropertyName,object Value)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif
InBlock.gif            
object Result=null;
InBlock.gif
InBlock.gif            Type myType 
= ClassInstance.GetType();
InBlock.gif
InBlock.gif            FieldInfo myFieldInfo 
= myType.GetField(ControlName, 
InBlock.gif                BindingFlags.NonPublic 
| BindingFlags.Instance | 
InBlock.gif                BindingFlags.Public 
| BindingFlags.Instance); 
InBlock.gif            
if(myFieldInfo != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                PropertyDescriptorCollection properties 
= TypeDescriptor.GetProperties(myType);
InBlock.gif                PropertyDescriptor myProperty 
= properties.Find(PropertyName, false);
InBlock.gif                
if(myProperty != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
dot.gif{
InBlock.gif                    
object ctr;
InBlock.gif                    ctr 
= myFieldInfo.GetValue(ClassInstance); 
InBlock.gif                    
try
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        myProperty.SetValue(ctr, Value);
InBlock.gif                        Result 
= ctr;
ExpandedSubBlockEnd.gif                    }

InBlock.gif                    
catch(Exception ex)
ExpandedSubBlockStart.gifContractedSubBlock.gif                    
dot.gif{
InBlock.gif                        MessageBox.Show(ex.Message,
"错误");
ExpandedSubBlockEnd.gif                    }

ExpandedSubBlockEnd.gif                }

ExpandedSubBlockEnd.gif            }

InBlock.gif            
return Result;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

哈哈,我还没找到原因,先记下来给大家看看!

转载于:https://www.cnblogs.com/RockyWang/archive/2005/12/06/291507.html

猜你喜欢

转载自blog.csdn.net/weixin_33969116/article/details/93685665