using System; using System.Reflection;public class MyClass { public int i = 10; public string k = "My Field"; public int MyMethod(int i) { this.i = i; return i; } public string MyMethod(string k) { this.k = k; return k; } } public class Type_GetMethod { public static void Main() { try { // Get the type of MyClass. Type myType = typeof(MyClass); // Get the attributes and metadata of MyMethod. MethodInfo myMethodInfo = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance, null, new Type[] {typeof(int)}, null); Console.WriteLine("\n Declaring type of the method {0} is: \n {1}", myMethodInfo, myMethodInfo.DeclaringType); // Get the attributes and metadata of MyMethod. MethodInfo myMethodInfo1 = myType.GetMethod("MyMethod", BindingFlags.Public | BindingFlags.Instance, null, new Type[] {typeof(string)}, null); Console.WriteLine("\n Declaring type of the method {0} is: \n {1}", myMethodInfo1, myMethodInfo1.DeclaringType); } catch(Exception e) { Console.WriteLine("Exception: {0}", e.Message); } } }
aiya这个说的对 Controls["label1"] 得到就是label1控件 不用那么麻烦
你拼一下你的labelname就可以了吧。 不一定要反射 for(int i = 1; i < n ;i ++){ labelName= string.Contact("label",i); 容器.Controls[labelName] 就是你的label了 }当然反射也是可以的
什么叫 控制控件??
如果你想得到对应名字的控件 可以从它的容器里得到
容器.Controls["label1"] 得到就是label1控件
using System.Reflection;public class MyClass
{
public int i = 10;
public string k = "My Field";
public int MyMethod(int i)
{
this.i = i;
return i;
}
public string MyMethod(string k)
{
this.k = k;
return k;
}
}
public class Type_GetMethod
{
public static void Main()
{
try
{
// Get the type of MyClass.
Type myType = typeof(MyClass);
// Get the attributes and metadata of MyMethod.
MethodInfo myMethodInfo = myType.GetMethod("MyMethod",
BindingFlags.Public | BindingFlags.Instance, null,
new Type[] {typeof(int)}, null);
Console.WriteLine("\n Declaring type of the method {0} is: \n {1}", myMethodInfo,
myMethodInfo.DeclaringType);
// Get the attributes and metadata of MyMethod.
MethodInfo myMethodInfo1 = myType.GetMethod("MyMethod",
BindingFlags.Public | BindingFlags.Instance, null,
new Type[] {typeof(string)}, null);
Console.WriteLine("\n Declaring type of the method {0} is: \n {1}", myMethodInfo1,
myMethodInfo1.DeclaringType);
}
catch(Exception e)
{
Console.WriteLine("Exception: {0}", e.Message);
}
}
}
Controls["label1"] 得到就是label1控件
不用那么麻烦
for(int i = 1; i < n ;i ++){
labelName= string.Contact("label",i);
容器.Controls[labelName] 就是你的label了
}当然反射也是可以的
你知道了对象明就没问题了,不用反射。
Controls["label1"]这就已经到label1这个对象了