以下通过反射获取Form1中的私有字段textBox1,并设置其文本。
using System;
using System.Collections.Generic;using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
setText();
}
public void setText()
{
Type type = GetType();
FieldInfo fieldInfo = type.GetField("textBox1", BindingFlags.Instance | BindingFlags.NonPublic);
TextBox textBox1 = (TextBox)fieldInfo.GetValue(this);
textBox1.Text = "11111";
}
}
}