static未央

博客园 首页 新随笔 联系 订阅 管理
通过反射来获取某个窗体注册过的事件
namespace WindowsFormsApplication1
{
    
public partial class Form1 : Form
    {
        
public Form1()   
        {   
            InitializeComponent();   
  
            
this.Load += new EventHandler(Form1_Load1);   
            
this.Load += new EventHandler(Form1_Load2);   
  
            PropertyInfo propertyInfo 
= (typeof(Form)).GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);   
            EventHandlerList eventHandlerList 
= (EventHandlerList)propertyInfo.GetValue(thisnull);   
            FieldInfo fieldInfo 
= (typeof(Form)).GetField("EVENT_LOAD", BindingFlags.Static | BindingFlags.NonPublic);   
  
            Delegate d 
= eventHandlerList[fieldInfo.GetValue(null)];   
  
            
if (d != null)   
            {   
                
foreach (Delegate de in d.GetInvocationList())   
                    MessageBox.Show(de.Method.Name);   
            }   
        }   
        
private void Form1_Load1(object sender, EventArgs e)   
        {   
            
//什么也不干   
        }   
        
private void Form1_Load2(object sender, EventArgs e)   
        {   
            
//什么也不干   
        }   
    }
}
posted on 2011-03-28 10:28  abstract未央  阅读(235)  评论(0编辑  收藏  举报