Search code examples
c#event-handlingdelegates

How can I add an existing delegate to an event?


I have an object that has events. I want to reset that object to default but keep the existing event subscriptions. My aim is to have a bunch of subscriptions to events on this object (_myRepo), then at some point I want to set _myRepo = null, create a new copy of the object with it's default state and then add back all the previously save subscribers. I use a generic approach instead of strong typing due to their being many repos in this class.

The issue I'm having is trying to add the subscription back to the event once it's has been recreated. I'm trying to do this in the ApplyCachedEventHandlers() function. How do I add this delegate back to the new object's event?

This is what I'm doing so far...

private IMyRepo _myRepo;
private Dictionary<Type, Delegate> _subscribers = new Dictionary<Type, Delegate>();

public IMyRepo MyRepo
{
    get
    {
        if (_myRepo == null)
            _myRepo = new MyRepo(_transaction);
        ApplyCachedEventHandlers(_myRepo);

        return _myRepo;
    }
}

private void ResetRepo()
{
    CacheEventHandlers(_myRepo);
    _myRepo = null;
}

private void CacheEventHandlers<T>(T repo)
{
    if (repo == null)
        return;

    Type t = repo.GetType();
    FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);

    foreach (FieldInfo fi in fia)
    {
        Delegate d = fi.GetValue(repo) as Delegate;
        if (d != null)
        {
            foreach (Delegate subscriber in d.GetInvocationList())
                _subscribers.Add(t, subscriber);
        }
    }
}

private void ApplyCachedEventHandlers<T>(T repo)
{
    if (repo == null || _subscribers == null || _subscribers.Count < 1)
        return;

    Type t = repo.GetType();
    FieldInfo[] fia = t.GetFields(BindingFlags.Static | BindingFlags.Instance | BindingFlags.NonPublic);

    foreach (KeyValuePair<Type, Delegate> subscriber in _subscribers)
    {
        if (t == subscriber.Key)
        {
            foreach (FieldInfo fi in fia)
            {
                // This is where I get hung up
                Debug.WriteLine("Add the saved delegate to the subscriptions of the event (fi).");
            }
        }
    }
}

Solution

  • If you want to subscribe to event via reflection you should not work with fields, but with events:

    var eventInfos = type.GetEvents(); // pass the flags if needed
    foreach (KeyValuePair<Type, Delegate> subscriber in _subscribers)
    {
        if (t == subscriber.Key)
        {
            foreach (EventInfo ei in eventInfos)
            {
                ei.AddEventHandler(repo, subscriber.Value); 
            }
        }
    }