- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个非常简单的类:
public delegate void ChangedEventHandler_OUT();
public class Plugin
{
public Plugin(string _pluginName)
{
pluginName = _pluginName;
}
public string pluginName;
public string member1;
public string member2;
public event ChangedEventHandler_OUT OnStatusChange1_OUT;
public event ChangedEventHandler_OUT OnStatusChange2_OUT;
public void PluginAction()
{
MessageBox.Show("ACtion from plugin " + pluginName);
}
}
在 Main 中,我创建了两个实例并连接它们的事件:
var plugin1 = new Plugin("plugin1") { member1 = "AAA1", member2="BBB1"};
var plugin2 = new Plugin("plugin2") { member1 = "AAA1", member2 = "BBB1" };
plugin1.OnStatusChange1_OUT += plugin2.PluginAction;
现在我完全意识到这没有什么意义,但我想深入反射(reflection)并获得我能获得的所有信息:
MEMBERS
lbxInfo.Items.Add("Plugin1 member:");
Type type = typeof(Plugin);
foreach (var field in type.GetFields())
{
string strName = field.Name; // Get string name
string strType = field.FieldType.Name;
string strValue = (string)field.GetValue(plugin1);
lbxInfo.Items.Add(strType + " " + strName + " = " + strValue);
}
现在我想获取有关事件的所有信息:
EVENTS:
lbxInfo.Items.Add("Plugin1 events:");
foreach (var ev in plugin1.GetType().GetEvents())
{
string strName = ev.Name;
string strType = ev.EventHandlerType.Name;
MethodInfo strConnectedTo = ev.GetAddMethod();
lbxInfo.Items.Add("Name = " + strName + " type =" + strType);
}
我没能做到的部分是理解什么事件与什么有关。简而言之,我想添加一个部分,告诉我哪些事件与哪些事件相关联,哪些不相关。像这样的东西:
Plugin1 OnStatusChange1_Out connectedTo plugin2.PluginAction OnStatusChange1_Out connectedTo NULL
这显然必须通过反射并在上面的同一个 foreach 循环中完成。
提前感谢您的帮助帕特里克
为 Jeroen van Langen 添加:
你写的有用。我试图将建议的解决方案放入反射循环中:
public void CheckEventInfo(Plugin plg, ChangedEventHandler_OUT ev)
{
string invocationList = null;
if (ev != null)
foreach (var item in ev.GetInvocationList())
invocationList += item.Method.Name;
}
然后
lbxInfo.Items.Add("Plugin1 events:");
foreach (var ev in plugin1.GetType().GetEvents())
{
string strName = ev.Name;
string strType = ev.EventHandlerType.Name;
MethodInfo strConnectedTo = ev.GetAddMethod();
CheckEventInfo( plugin1,ev);<------NO!
lbxInfo.Items.Add("Name = " + strName + " type =" + strType);
}
所以在这里我不知道该放什么,因为 ev 是一个事件信息而不是 ChangedEventHandler_OUT。请你帮助我好吗?谢谢
最佳答案
我想您只是出于教育目的而这样做。一般来说,如果您想知道某个特定事件是否有任何订阅者,或者更糟的是,想知道订阅者的确切列表,那么您就犯了架构错误。 (事件调用方法存在一个异常(exception),它应该在调用之前检查事件委托(delegate)是否为空。)
这是获得所需结果的方法。
string GetSubscriptions(object o)
{
Type t = o.GetType();
// Obtain the collection of EventInfo's
var events = t.GetEvents();
if (events.Length == 0)
{
return "No events in " + t.Name;
}
string result = string.Empty;
foreach (var item in events)
{
result += "Event " + item.Name + ": ";
// Get the event's backing field description (FieldInfo)
var ed = t.GetField(item.Name, BindingFlags.Instance | BindingFlags.NonPublic);
if (ed == null)
{
throw new InvalidOperationException("Event backing field could not be obtained");
}
// Get the value of the backing field
var dl = ed.GetValue(o) as Delegate;
// If value is not null, then we've subscriptions
if (dl != null)
{
// Get the invocation list - an array of Delegate
var il = dl.GetInvocationList();
// This check is actually not needed, since the array should always contain at least 1 item
if (il.Length != 0)
{
// Use Target property of the delegate to get a reference to the object the delegate's method belongs to
result += string.Join("; ", il.Select(i => i.Target.GetType().Name + "." + i.Method.Name)) + Environment.NewLine;
continue;
}
}
result += "no subscriptions" + Environment.NewLine;
}
return result;
}
您必须记住,对于静态事件,Target
属性可以为 null
。要也获取静态事件,请将 BindingFlags.Static
包含到 GetField() 方法的第二个参数中。
关于c# - 如何通过反射获取事件目标方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37831513/
我需要您在以下方面提供帮助。近一个月来,我一直在阅读有关任务和异步的内容。 我想尝试在一个简单的 wep api 项目中实现我新获得的知识。我有以下方法,并且它们都按预期工作: public Htt
我的可执行 jar 中有一个模板文件 (.xls)。不需要在运行时我需要为这个文件创建 100 多个副本(稍后将唯一地附加)。用于获取 jar 文件中的资源 (template.xls)。我正在使用
我在查看网站的模型代码时对原型(prototype)有疑问。我知道这对 Javascript 中的继承很有用。 在这个例子中... define([], function () { "use
影响我性能的前三项操作是: 获取滚动条 获取偏移高度 Ext.getStyle 为了解释我的应用程序中发生了什么:我有一个网格,其中有一列在每个单元格中呈现网格。当我几乎对网格的内容做任何事情时,它运
我正在使用以下函数来获取 URL 参数。 function gup(name, url) { name = name.replace(/[\[]/, '\\\[').replace(/[\]]/,
我最近一直在使用 sysctl 来做很多事情,现在我使用 HW_MACHINE_ARCH 变量。我正在使用以下代码。请注意,当我尝试获取其他变量 HW_MACHINE 时,此代码可以完美运行。我还认为
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 关闭 9 年前。 要求提供代码的问题必须表现出对所解决问题的最低限度的理解。包括尝试过的解决方案、为什么
由于使用 main-bower-files 作为使用 Gulp 的编译任务的一部分,我无法使用 node_modules 中的 webpack 来require 模块code> dir 因为我会弄乱当
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 5 年前。 Improve this qu
我使用 Gridlayout 在一行中放置 4 个元素。首先,我有一个 JPanel,一切正常。对于行数变大并且我必须能够向下滚动的情况,我对其进行了一些更改。现在我的 JPanel 上添加了一个 J
由于以下原因,我想将 VolumeId 的值保存在变量中: #!/usr/bin/env python import boto3 import json import argparse import
我正在将 MSAL 版本 1.x 更新为 MSAL-browser 的 Angular 。所以我正在尝试从版本 1.x 迁移到 2.X.I 能够成功替换代码并且工作正常。但是我遇到了 acquireT
我知道有很多关于此的问题,例如 Getting daily averages with pandas和 How get monthly mean in pandas using groupby但我遇到
This is the query string that I am receiving in URL. Output url: /demo/analysis/test?startDate=Sat+
我正在尝试使用 javascript 中的以下代码访问 Geoserver 层 var gkvrtWmsSource =new ol.source.ImageWMS({ u
API 需要一个包含授权代码的 header 。这就是我到目前为止所拥有的: var fullUrl = 'https://api.ecobee.com/1/thermostat?json=\{"s
如何获取文件中的最后一个字符,如果是某个字符,则删除它而不将整个文件加载到内存中? 这就是我目前所拥有的。 using (var fileStream = new FileStream("file.t
我是这个社区的新手,想出了我的第一个问题。 我正在使用 JSP,我成功地创建了 JSP-Sites,它正在使用jsp:setParameter 和 jsp:getParameter 具有单个字符串。
在回答 StoreStore reordering happens when compiling C++ for x86 @Peter Cordes 写过 For Acquire/Release se
我有一个函数,我们将其命名为 X1,它返回变量 Y。该函数在操作 .on("focusout", X1) 中使用。如何获取变量Y?执行.on后X1的结果? 最佳答案 您可以更改 Y 的范围以使其位于函
我是一名优秀的程序员,十分优秀!