【发布时间】:2011-03-15 15:05:55
【问题描述】:
考虑一个有一些事件的类。这个事件列表将会增长。有些是可选的。其他是必需的。
为了简化一些初始验证,我有一个自定义属性,将事件标记为必需事件。 例如:
[RequiredEventSubscription("This event is required!")]
public event EventHandler ServiceStarted;
到目前为止一切顺利。 为了验证所有事件,我使用反射迭代事件列表并获取自定义属性。 但我需要一种方法来确定该事件是否被订阅。
无需反射,ServiceStarted.GetInvocationList 就可以完成这项工作。但该事件必须来自此列表: var eventList = this.GetType().GetEvents().ToList();
有没有办法检查事件列表中的给定事件是否使用反射订阅?
--[更新]-- 这是基于 Ami 的回答的可能解决方案:
private void CheckIfRequiredEventsAreSubscribed()
{
var eventList = GetType().GetEvents().ToList().Where(e => Attribute.IsDefined(e, typeof(RequiredEventSubscription)));
StringBuilder exceptionMessage = new StringBuilder();
StringBuilder warnMessage = new StringBuilder();
foreach (var evt in eventList)
{
RequiredEventSubscription reqAttr = (RequiredEventSubscription) evt.GetCustomAttributes(typeof(RequiredEventSubscription), true).First();
var evtDelegate = this.GetType().GetField(evt.Name, BindingFlags.Instance | BindingFlags.NonPublic);
if (evtDelegate.GetValue(this) == null)
{
warnMessage.AppendLine(reqAttr.warnMess);
if (reqAttr.throwException) exceptionMessage.AppendLine(reqAttr.warnMess);
}
}
if (warnMessage.Length > 0)
Console.WriteLine(warnMessage);
if (exceptionMessage.Length > 0)
throw new RequiredEventSubscriptionException(exceptionMessage.ToString());
}
非常感谢!!
【问题讨论】:
-
恕我直言,如果需要订阅事件,首先不要将其设为事件,而是将其作为类构造函数的委托参数。
-
什么在做验证,在这种情况下“必需”是什么意思?谁/什么需要?
-
@Daniel,这是我的第一个方法,但是在整个代码中发送了这么多的委托导致我进行了这个实验。
-
@David,现在验证过程只需要记录一个警告。
标签: c# events reflection subscription