3

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

Community
  • 1
  • 1
Sinatr
  • 20,892
  • 15
  • 90
  • 319
  • 2
    Could you please write your code as text in the question? – hendryanw Apr 20 '16 at 09:24
  • Normally I do, but why do you need the code in this case? To reproduce the problem? Really? Or in case picture will rot? – Sinatr Apr 20 '16 at 09:26
  • Possible duplicate of [C# - static types cannot be used as type arguments](http://stackoverflow.com/questions/5858591/c-sharp-static-types-cannot-be-used-as-type-arguments) – hendryanw Apr 20 '16 at 09:29
  • @Hendry, I have that question answer mentioned in my question. – Sinatr Apr 20 '16 at 09:30
  • @Sinatr - The edit is an improvement, the code needs to be discoverable by Google. But you left out the error message. – H H Apr 20 '16 at 09:31
  • You are correct, but I think the accepted answer in the linked question perfectly explains it. – hendryanw Apr 20 '16 at 09:32
  • 1
    @HenkHolterman, I see the point now, thanks. – Sinatr Apr 20 '16 at 09:35

1 Answers1

5

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.

Dennis
  • 37,026
  • 10
  • 82
  • 150
  • I thought `WeakEventManager` will use `null` as key and perform differently for `static` and normal events. It seems instead it perform similarly, using `StaticSource` instance for static events. Thanks. Regarding language limitation, this is simply pity, required to remove `static` from pure static classes (no instance members) only to be able to subscribe to its events via `WeakEventManager`. From other point writing non-generic event manager for each static type is even more troublesome. – Sinatr Apr 20 '16 at 09:50
  • I'd bet, that primary goal for generic `WeakEventManager` is to help to handle instance events. In fact, static events are rather rare things. If your code is full of static events and/or static classes, it is better to refactor your code instead of inventing workarounds – Dennis Apr 20 '16 at 09:59
  • There's a good solution on this page https://stackoverflow.com/questions/8876112/using-weakeventmanager-with-a-static-event – John Stewien Jul 28 '21 at 05:28