Class EventNameExtractor
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
Namespace: Umbraco.Cms.Core.Events
Assembly: Umbraco.Core.dll
Syntax
public class EventNameExtractor
Constructors
View SourceEventNameExtractor()
Declaration
public EventNameExtractor()
Methods
View SourceFindEvent(object, object, Func<string, bool>)
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 |
|---|---|---|
| object | sender | |
| object | args | |
| Func<string, bool> | exclude | A filter to exclude matched event names, this filter should return true to exclude the event name from being matched |
Returns
| Type | Description |
|---|---|
| Attempt<EventNameExtractorResult> | null if not found or an ambiguous match |
FindEvent(Type, Type, Func<string, bool>)
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<string, bool> | exclude | A filter to exclude matched event names, this filter should return true to exclude the event name from being matched |
Returns
| Type | Description |
|---|---|
| Attempt<EventNameExtractorResult> | null if not found or an ambiguous match |
FindEvents(Type, Type, Func<string, bool>)
Finds all events on the sender type that match the args type.
Declaration
public static string[] FindEvents(Type senderType, Type argsType, Func<string, bool> exclude)
Parameters
| Type | Name | Description |
|---|---|---|
| Type | senderType | The type of the event sender. |
| Type | argsType | The type of the event arguments. |
| Func<string, bool> | exclude | A filter to exclude matched event names. Return true to exclude. |
Returns
| Type | Description |
|---|---|
| string[] | An array of matching event names. |
MatchIngNames(string)
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 |
|---|---|---|
| string | eventName |
Returns
| Type | Description |
|---|---|
| bool |
MatchNonIngNames(string)
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 |
|---|---|---|
| string | eventName |
Returns
| Type | Description |
|---|---|
| bool |