0

I have a piece of code that follows the Observer pattern. Each publisher creates events and invokes them accordingly for subscribers to consume. I thought of creating an EventRegistry where all events are registered (as dictionaries <Enum, event>). That way both subscribers and publishers have a central place to know what events are available, instead of having to import each file where the event was created.

I tried the code below but it doesn't work. I implemented the registry as a singleton (following this implementation), so events are created only once. I'm not too familiar with C#, so I'm not sure what's wrong. The error I get:

A field initializer cannot reference the non-static field, method, or property 'EventRegistry.Event1' [Assembly-CSharp]

The code:

using System;
using System.Collections.Generic;
using MyNamespace;

namespace MyNamespace
{
    class EventRegistry
    {
        private EventRegistry() { }

        private static EventRegistry _instance;

        private static readonly object _lock = new object();

        public static EventRegistry GetInstance()
        {
            if (_instance == null)
            {
                lock (_lock)
                {
                    if (_instance == null)
                    {
                        _instance = new EventRegistry();
                    }
                }
            }
            return _instance;
        }

        private enum Publisher1 {
            Event1,
        }
        private event EventHandler Event1;

        public Dictionary<Publisher1, EventHandler> Publisher1Events {get;} = new Dictionary<Publisher1, EventHandler> {
            {Publisher1.Event1, Event1}
        };
    }
}

I'm aware that Singletons are evil. I think this is a good use case for it though, but if there's a better way feel free to suggest otherwise!

Victor
  • 1,163
  • 4
  • 25
  • 45

1 Answers1

1

You have to initialize Publisher1Events in your private constructor, like:

private EventRegistry()
{
    Publisher1Events = new Dictionary<Publisher1, EventHandler> {
        {Publisher1.Event1, Event1}
    };
}

And you have to make the Publisher1 enum public.

The reason you have to initialize in the constructor is because field initializers require constant or static values.

Hayden
  • 2,902
  • 2
  • 15
  • 29