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); } }
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
具体的语句如下
this.Controls["Button1"].Text="abcd";
------------------
反射是用来读取并解析程序集的元数据的,不是用来引用类的实例的
还是错了!不过还是要谢谢你的关心,楼主可能有??
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 ;
}
}
}
}
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 ;
}
}
}
}
楼上的方法首先需要 FindControl,这样就只能操作 Control 的派生类了,但是其他对象就不能用这个方法了。有时我也要设置其他对象的属性。我需要一种通过字符串返回任意对象的实例的方法。IDesignerSerializationManager 接口有个 GetInstance 方法可以达到我的要求,但是怎样弄呢?
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);
}
}