I am trying to get used to WeakEventManager
and I stumble with following:
The only difference between A
and B
is static
, please ignore copy/paste error with nameof
;)
I found this answer regarding generics and static types, but I wonder what WeakEventManager
is doing with A
then? Somehow it can work with null
as source of static event.
I am seeking for a simple answer why static
event is ok, but static class
as TEventSource
suddenly is not.
Code:
public class A
{
public static event EventHandler Event;
}
public static class B
{
public static event EventHandler Event;
}
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
WeakEventManager<A, EventArgs>.AddHandler(null, nameof(A.Event), (s, e) => { });
WeakEventManager<B, EventArgs>.AddHandler(null, nameof(B.Event), (s, e) => { });
}
}
Error:
Error CS0718
'B': static types cannot be used as type arguments
WeakEventManager
can deal with static events, when source
is null:
object sourceKey = (source != null) ? source : StaticSource;
where StaticSource
is a special "event source" for static events.
This is implementation details of WeakEventManager
, and that's why it's OK.
About static types as generic parameter - this is language limitation. It isn't related specifically to WeakEventManager
.