题
我有一个情况,当我需要访问一个类的私有成员在嵌入式私有类。我怎样才能有效地做到这一点。
public partial class Form1 : Form
{
// this private label will be used only in this form
private class MyFormLabel : Label
{
MyFormLabel()
{
this.BorderStyle = BorderStyle.FixedSingle;
// ?? how to pass the from label_Click (without delegates)?
this.Click +=new EventHandler(????);
}
}
public Form1()
{
InitializeComponent();
}
private void label_Click(object sender, EventArgs e)
{
// displays the form caption
MessageBox.Show(this.Text);
}
}
NotaBene: 我添加动态控件到窗体,所以我会确保在创建后,他们已经订阅了该事件。
解决方案
可以从嵌套类访问的类私有成员。当然,访问一个实例方法仍然需要的类的一个实例。
其他提示
在这种情况下,只是做它的其他方式,l.e.g. InitializeComponent()
后做myFormLabel.Click += label_Click
不隶属于 StackOverflow