foreach(Control ctl in this.Controls)
{
if(ctl.Name = "test")
{
object m_object = ctl ;
}
}

解决方案 »

  1.   

    Activator 类
    ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfSystemActivatorClassTopic.htm反射:
    http://www.c-sharpcenter.com/CSNET/dynamicinvoke.asp
    http://www.c-sharpcorner.com/Code/2002/April/LoadingAssemblyInfo.asp
      

  2.   

    楼主参考Activator.CreateInstance方法
      

  3.   

    谢谢各位的提示,但是问题还是没有解决。首先 button1 的实例已经被建立,我不想通过 Activator.CreateInstance 再次建立了,而且在运行期我只想通过字符串的形式如 "button1" 来改变 button1 的属性,这样类型就是未知的了,也不能通过 Activator.CreateInstance 来建立。我注意到设计期有个 IDesignerSerializationManager 接口,它有个 GetInstance 方法可以实现我的要求,但是在运行时刻不知如何去实现同样的功能...
      

  4.   

    Form.Controls属性
    具体的语句如下
    this.Controls["Button1"].Text="abcd";
      

  5.   

    ((Button)this.Controls["Button1"]).Text="abcd";
    ------------------
    反射是用来读取并解析程序集的元数据的,不是用来引用类的实例的
      

  6.   

    zjsen(星愿)(个人观点,仅供参考.请自行验证)
    还是错了!不过还是要谢谢你的关心,楼主可能有??
      

  7.   

    如果是我的话。也用Samen168(Samen)的方法。不过改进了一下:
    private void FindControl(Control control)
    {
    foreach(Control ctl in control.Controls)
    {
             if(ctl.Controls.count>1)
             {
                     FindControl(ctl);
             }
             else
             {
            if(ctl.Name = "test")
            {
            return ctl ;
            }
             }
    }
    }
      

  8.   

    啊。我的程序写错了返回值不应该是空的。
    private Control FindControl(Control control)
    {
    foreach(Control ctl in control.Controls)
    {
             if(ctl.Controls.count>1)
             {
                     return FindControl(ctl);
             }
             else
             {
            if(ctl.Name = "test")
            {
            return ctl ;
            }
             }
    }
    }
      

  9.   


    楼上的方法首先需要 FindControl,这样就只能操作 Control 的派生类了,但是其他对象就不能用这个方法了。有时我也要设置其他对象的属性。我需要一种通过字符串返回任意对象的实例的方法。IDesignerSerializationManager 接口有个 GetInstance 方法可以达到我的要求,但是怎样弄呢?
      

  10.   

    我想IDesignerSerializationManager也不一定能满足你和要求。因为GetInstance()方法能得到的实例是通过CreateInstance()加进来的。实现这个功能。就要先把你的对象加到序列里。
      

  11.   

    using System;
    using System.Collections;
    using System.Windows.Forms;public class HashtableTest
    {
      public static void Main()  
      {
        Button  b1= new Button();
        Button  b2= new Button();
        ListBox lb3 = new ListBox();    Hashtable myHT = new Hashtable();
        myHT.Add("First",  b1);
        myHT.Add("Second", b2);
        myHT.Add("Third",  lb3);
        string s = "First";
        Console.WriteLine(myHT[s]);
        Console.WriteLine(myHT["Second"]);
        Console.WriteLine(myHT["Third"]); 
        foreach(object v in myHT.Values)
          Console.WriteLine(v);
      }
    }