天天看點

C# 反射事件

反射出事件清單的方法

如果是 自己寫的類直接反射成員就可以擷取到委托集合了

例如

  public class Test

        {

            public delegate void Show();

            public event Show OK;

        }

我們在代碼中寫

  private void button1_Click(object sender, EventArgs e)

        {

            Test _TestEvent = new Test();

            _TestEvent.OK += new Test.Show(_TestEvent_OK1);

            _TestEvent.OK += new Test.Show(_TestEvent_OK2);

            Delegate[] _DelegateList= GetObjectEventList(_TestEvent, "OK");

            if (_DelegateList != null)

            {

                for (int i = 0; i != _DelegateList.Length; i++)

                {

                    _DelegateList[i].DynamicInvoke(new object[] { });

                }

            }

        }

        void _TestEvent_OK1()

        {

            MessageBox.Show("OK1");

        }

        void _TestEvent_OK2()

        {

            MessageBox.Show("OK2");

        }      

這樣我們可以靈活的擷取綁定的事件,并且調用。

雖然有點違背了設計。但有時候還是有點用的。

下面是WINFORN控件的擷取

        private void button1_Click(object sender, EventArgs e)

        {

            PictureBox _PictureBox = new PictureBox();

            _PictureBox.Click += new EventHandler(_PictureBox_Click1);

            _PictureBox.Click += new EventHandler(_PictureBox_Click2);

            Delegate[] _ControlDelegate = GetObjectEventList(_PictureBox, "EventClick");

            if (_ControlDelegate != null)

            {

                for (int i = 0; i != _ControlDelegate.Length; i++)

                {

                    _ControlDelegate[i].DynamicInvoke(new object[] { _PictureBox,null});

                }

            }         

        }

        void _PictureBox_Click1(object sender, EventArgs e)

        {

            MessageBox.Show("ok1");

        }

        void _PictureBox_Click2(object sender, EventArgs e)

        {

            MessageBox.Show("ok2");

        }

一般來說控件事件前加個 Event ..例如 DoubleClick事件 前加個Event 用EventDoubleClick來擷取..

/// <summary>

/// 擷取對象事件 [email protected] qq:116149

/// </summary>

/// <param name="p_Object">對象</param>

/// <param name="p_EventName">事件名</param>

/// <returns>委托列</returns>

public Delegate[] GetObjectEventList(object p_Object, string p_EventName)

{

System.Reflection.FieldInfo _Field = p_Object.GetType().GetField(p_EventName, BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public | BindingFlags.Static);

if (_Field == null)

{

return null;

}

object _FieldValue = _Field.GetValue(p_Object);

if (_FieldValue != null && _FieldValue is Delegate)

{

Delegate _ObjectDelegate = (Delegate)_FieldValue;

return _ObjectDelegate.GetInvocationList();

}

return null;

}

/// <summary>

/// 擷取控件事件 [email protected] qq:116149

/// </summary>

/// <param name="p_Control">對象</param>

/// <param name="p_EventName">事件名 EventClick EventDoubleClick </param>

/// <returns>委托列</returns>

public Delegate[] GetObjectEventList(Control p_Control, string p_EventName)

{

PropertyInfo _PropertyInfo = p_Control.GetType().GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);

if (_PropertyInfo != null )

{

object _EventList =_PropertyInfo.GetValue(p_Control, null);

if (_EventList != null && _EventList is EventHandlerList)

{

EventHandlerList _List = (EventHandlerList)_EventList;

FieldInfo _FieldInfo = (typeof(Control)).GetField(p_EventName, BindingFlags.Static | BindingFlags.NonPublic);

if (_FieldInfo == null) return null;

Delegate _ObjectDelegate = _List[ _FieldInfo.GetValue(p_Control)];

if (_ObjectDelegate == null) return null;

return _ObjectDelegate.GetInvocationList();

}

}

return null;

}