View Source
There is actually no way to discover an event name in c# at the time of raising the event. It is possible
to get the event name from the handler that is being executed based on the event being raised, however that is not
what we want in this case. We need to find the event name before it is being raised - you would think that it's
possible
with reflection or anything but that is not the case, the delegate that defines an event has no info attached to
it, it
is literally just an event.
So what this does is take the sender and event args objects, looks up all public/static events on the sender that
have
a generic event handler with generic arguments (but only) one, then we match the type of event arguments with the
ones
being passed in. As it turns out, in our services this will work for the majority of our events! In some cases it
may not
work and we'll have to supply a string but hopefully this saves a bit of magic strings.
We can also write tests to validate these are all working correctly for all services.
Inheritance
System.Object
Assembly: Umbraco.Core.dll
public class EventNameExtractor
Methods
View Source
Finds the event name on the sender that matches the args type
Declaration
public static Attempt<EventNameExtractorResult> FindEvent(object sender, object args, Func<string, bool> exclude)
Parameters
Type |
Name |
Description |
System.Object |
sender |
|
System.Object |
args |
|
Func<System.String, System.Boolean> |
exclude |
A filter to exclude matched event names, this filter should return true to exclude the event name from being
matched
|
Returns
View Source
Finds the event name on the sender that matches the args type
Declaration
public static Attempt<EventNameExtractorResult> FindEvent(Type senderType, Type argsType, Func<string, bool> exclude)
Parameters
Type |
Name |
Description |
Type |
senderType |
|
Type |
argsType |
|
Func<System.String, System.Boolean> |
exclude |
A filter to exclude matched event names, this filter should return true to exclude the event name from being
matched
|
Returns
View Source
Declaration
public static string[] FindEvents(Type senderType, Type argsType, Func<string, bool> exclude)
Parameters
Type |
Name |
Description |
Type |
senderType |
|
Type |
argsType |
|
Func<System.String, System.Boolean> |
exclude |
|
Returns
Type |
Description |
System.String[] |
|
View Source
Return true if the event is named with an ING name such as "Saving" or "RollingBack"
Declaration
public static bool MatchIngNames(string eventName)
Parameters
Type |
Name |
Description |
System.String |
eventName |
|
Returns
Type |
Description |
System.Boolean |
|
View Source
Return true if the event is not named with an ING name such as "Saving" or "RollingBack"
Declaration
public static bool MatchNonIngNames(string eventName)
Parameters
Type |
Name |
Description |
System.String |
eventName |
|
Returns
Type |
Description |
System.Boolean |
|