Search code examples
c#eventseventhandler

check event argument in event handler c#


I want to keep track of the argument passed in argument in event subscription, so that I can after select/narrow down my invokes.

public List<Tags> _toSpecificTagSubscribed = new List<Tags>();
private event Action<Tags> _onSpecificTagEvent;
public event Action<Tags> OnSpecificTagEvent {
    add { 
        _onSpecificTagCollision += value; 
        if (!_toSpecificTagSubscribed.Contains(<TagArgumentValue>))
            _toSpecificTagSubscribed.Add(<TagArgumentValue>); 
        }
        remove { _onSpecificTagEvent -= value; }
    }
}

See the <TagArgumentValue>. That is passed in in the event itself on the subscription so I wonder how to access it. Would be kind of value.<TagArgumentValue>, meaning the value of the argument passed into the event.

Is this possible? How?


Solution

  • If I understand you correctly, you're confusing "the information provided when the event is raised" with "the information provided within the event handler".

    An Action<Tags> can do anything with the Tags it is provided. It might choose to only respond to certain tags, but it's just arbitrary code.

    If you want to have a way of subscribing just for a specific tag, you'd need that to be part of the subscription information, e.g.

    public void AddTagHandler(Tags tag, Action<Tags> handler)
    

    You could potentially use a Dictionary<Tags, Action<Tags>> to keep track of the handlers subscribed for any given tag.

    (You might also want to check that Tags is the right name for the type - it looks like it should represent a single tag rather than a collection of tags, given how you're using it.)