Add RaiseEvent generic extension method
- Dominant language
- C#
- Stars
- 7.7k
- Forks
- 1.3k
- Avg merge
- 1d 11h
- Merged PRs (30d)
- 61
Description
Add a convenience extension method allowing for contemporary and planned language level features to be more prominently used.
```csharp
namspace System.Windows
{
// EventManager is already static, so this seems like the best placement for it
public static class EventManager
{
public static void RaiseEvent(this DependencyObject source, TArguments e)
where TArguments : RoutedEventArgs => source.RaiseEvent(e);
// Then if folks wish to be a little more strict about the arguments, they can be
public static void RaiseEvent(this TSource source, TArguments e)
where TSource : DependencyObject
where TArguments : RoutedEventArgs => source.RaiseEvent(e);
}
}
```
And in practical usage, assuming appropriate event declarations, etc:
```csharp
public class BubblesManager
{
public static void RaiseEvent(DependencyObject source, int bubbles)
=> source.RaiseEvent(new(BubblingEvent, source, bubbles));
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
```
For sake of example:
```csharp
public class BubblesRoutedEventArgs : RoutedEventArgs
{
public int Bubbles { get; }
internal BubblesRoutedEventArgs(RoutedEvent routedEvent, object source, int bubbles)
: base(routedEvent, source)
// ^^^^^^^^^^^ ^^^^^^
{
Bubbles = bubbles;
}
}
```
Tangentially, although at first glance, it seems counterintuitive why we would need to relay the `RoutedEventArgs.RoutedEvent`, especially given the `event` mechanism. Might seem better to do something like:
```csharp
source.RaiseEvent(BubblesEvent, e);
```
And then since DependencyObject is handling the decoupled bookkeeping for us, we can focus more on the arguments properties themselves without having to also concern ourselves with the bookkeeping, even in raising the event.
Contributor guide
Assessment
This issue has not been assessed yet.