lor*_*ert 4 c# events delegates
我坐在我的电脑前面想知道如何获得所有添加的事件.我刚看了一些文章,包括AC#Bedtime Story,以便更好地了解事件,我认为我现在有了主要的想法.但是我仍然无法弄清楚如何触发事件被触发时执行的方法/委托列表.实际上在我的情况下,如果我知道是否有任何方法/代表被分配给某个事件就足够了.例如:我正在使用Gma.UserActivityMonitor(用于键盘/鼠标挂钩)现在我想知道事件HookManager.KeyUp事件是否为空.如果为null,则添加委托喊.在我的情况下这一个\ /
HookManager.KeyUp += new KeyEventHandler(HookManager_KeyUp);
Run Code Online (Sandbox Code Playgroud)
示例代码
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;
using Gma.UserActivityMonitor;
namespace EventTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
HookManager.KeyUp += new KeyEventHandler(HookManager_KeyUp);
Delegate[] a = GetEventSubscribers(button1, "Click");
label1.Text = a[0].Method.ToString();
}
void HookManager_KeyUp(object sender, KeyEventArgs e)
{
/* Do something*/
}
bool NoEventAttached()
{
return false;
}
public static Delegate[] GetEventSubscribers(object target, string eventName)
{
Type t = target.GetType();
var eventInfo = t.GetEvent(eventName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
do
{
FieldInfo[] fia = t.GetFields(
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic);
foreach (FieldInfo fi in fia)
{
if (fi.Name == eventName)
{
Delegate d = fi.GetValue(target) as Delegate;
if (d != null)
return d.GetInvocationList();
}
else if (fi.FieldType == typeof(EventHandlerList))
{
----> var obj = fi.GetValue(target) as EventHandlerList;
var eventHandlerFieldInfo = obj.GetType().GetField("head", BindingFlags.Instance | BindingFlags.NonPublic);
do
{
var listEntry = eventHandlerFieldInfo.GetValue(obj);
var handler = listEntry.GetType().GetField("handler", BindingFlags.Instance | BindingFlags.NonPublic);
if (handler != null)
{
var subD = handler.GetValue(listEntry) as Delegate;
if (subD.GetType() != eventInfo.EventHandlerType)
{
eventHandlerFieldInfo = listEntry.GetType().GetField("next", BindingFlags.Instance | BindingFlags.NonPublic);
obj = listEntry as EventHandlerList; <-----------
continue;
}
if (subD != null)
{
return subD.GetInvocationList();
}
}
}
while (eventHandlerFieldInfo != null);
}
}
t = t.BaseType;
} while (t != null);
return new Delegate[] { };
}
private void button1_Click(object sender, EventArgs e)
{
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
}
}
}
Run Code Online (Sandbox Code Playgroud)
请注意此控制台应用程序,特别是线路调用GetInvocationList().该方法返回附加的委托列表.每个人Delegate都有一个Method属性.每个Method属性都有一个Name.
internal class Program
{
public event EventHandler CheckInvocationList;
private static void Main(string[] args)
{
Program p = new Program();
p.CheckInvocationList += new EventHandler(p_CheckInvocationList);
p.Method1();
Console.WriteLine(string.Join(" | ", p.CheckInvocationList.GetInvocationList().Select(d => d.Method.Name).ToArray()));
}
static void p_CheckInvocationList(object sender, EventArgs e)
{
throw new NotImplementedException();
}
public void Method1()
{
this.CheckInvocationList += new EventHandler(Program_CheckInvocationList);
}
void Program_CheckInvocationList(object sender, EventArgs e)
{
throw new NotImplementedException();
}
}
Run Code Online (Sandbox Code Playgroud)
好吧,这有点乱,但它可以得到你想要的答案.即使内部实现使用了EventHandlerList(这有点常见),但我无法保证它绝对适用于所有情况,因为可能存在许多不同的内部表示.
但是,希望这对你有用.正如您在示例中所看到的,我使用BackgroundWorker该类作为我的示例类来获取我不拥有的特定事件的调用列表.
using System;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
internal class Program
{
private static void Main(string[] args)
{
BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += new DoWorkEventHandler(worker_DoWork);
worker.DoWork += new DoWorkEventHandler(worker_DoWork2);
worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted);
worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged);
Console.WriteLine(string.Join(" | ", GetEventSubscribers(worker, "DoWork").Select(d => d.Method.Name).ToArray()));
}
static void worker_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
throw new NotImplementedException();
}
static void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
throw new NotImplementedException();
}
static void worker_DoWork2(object sender, DoWorkEventArgs e)
{
throw new NotImplementedException();
}
static void worker_DoWork(object sender, DoWorkEventArgs e)
{
throw new System.NotImplementedException();
}
public static Delegate[] GetEventSubscribers(object target, string eventName)
{
Type t = target.GetType();
var eventInfo = t.GetEvent(eventName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Static);
do
{
FieldInfo[] fia = t.GetFields(
BindingFlags.Static |
BindingFlags.Instance |
BindingFlags.NonPublic);
foreach (FieldInfo fi in fia)
{
if (fi.Name == eventName)
{
Delegate d = fi.GetValue(target) as Delegate;
if (d != null)
return d.GetInvocationList();
}
else if (fi.FieldType == typeof(EventHandlerList))
{
dynamic obj = fi.GetValue(target) as EventHandlerList;
var eventHandlerFieldInfo = obj.GetType().GetField("head", BindingFlags.Instance | BindingFlags.NonPublic);
do
{
var listEntry = eventHandlerFieldInfo.GetValue(obj);
var handler = listEntry.GetType().GetField("handler", BindingFlags.Instance | BindingFlags.NonPublic);
if (handler != null)
{
var subD = handler.GetValue(listEntry) as Delegate;
if (subD.GetType() != eventInfo.EventHandlerType)
{
eventHandlerFieldInfo = listEntry.GetType().GetField("next", BindingFlags.Instance | BindingFlags.NonPublic);
obj = listEntry;
continue;
}
if (subD != null)
{
return subD.GetInvocationList();
}
}
}
while (eventHandlerFieldInfo != null);
}
}
t = t.BaseType;
} while (t != null);
return new Delegate[] { };
}
}
Run Code Online (Sandbox Code Playgroud)
最后,虽然我对代码进行了一些非常大的修改,但我必须赞扬鲍勃鲍威尔让我开始这个.
| 归档时间: |
|
| 查看次数: |
1186 次 |
| 最近记录: |