我也来玩玩,虽然与LZ不是同一个意思,不过同样是泛型的使用:[C#] 纯文本查看 复制代码 namespace ClearTexts {
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
}
public static void ClearText<T>(T t) {
var type = t.GetType();
if (type.GetProperty("Text") != null) {
type.GetProperty("Text").SetValue(t, string.Empty, null);
}
if (type.GetProperty("Controls") != null) {
var controls = (type.GetProperty("Controls").GetValue(t, null) as Control.ControlCollection);
foreach (Control item in controls) {
ClearText(item);
}
}
}
private void button1_Click(object sender, EventArgs e) {
ClearText(textBox3);
}
private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
ClearText(groupBox1);
}
}
}
|