110,567
社区成员
发帖
与我相关
我的任务
分享
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
oth.Eve += new Action(Method3);
}
void Method3()
{
MessageBox.Show("3");
}
Other oth = new Other();
private void button1_Click(object sender, EventArgs e)
{
oth.Eve -= Method3;//Method3可见,直接移除
//Other是引用的其他类库,根本不知道Eve订阅了哪些方法
}
private void button2_Click(object sender, EventArgs e)
{
oth.Begin();
}
}
class Other//此类不能修改!
{
public Other()
{
Eve += new Action(Method1);
Eve += new Action(Method2);
}
public event Action Eve;
public void Begin()
{
if (Eve != null)
Eve();
}
void Method1()
{
MessageBox.Show("1");
}
void Method2()
{
MessageBox.Show("2");
}
}
private void button1_Click(object sender, EventArgs e)
{
EventInfo ei = oth.GetType().GetEvent("Eve");
FieldInfo fi = oth.GetType().GetField("Eve", BindingFlags.NonPublic | BindingFlags.Instance);
Action act = (Action)fi.GetValue(oth);
ei.RemoveEventHandler(oth, act);
//oth.Eve -= Method3;
}