Suppose I want to create a set of observers based on type. That is to say, when they are notified of an event, they are told the type of one of the arguments and then decides whether or not to act based on if it can operate on that type.
Are there any simple ways to do this? I figured this would be fairly simple to do with generics, but that seems to be turning out to be harder than I imagined. And I would prefer not to have to deal with casting a bunch of references to object if I can avoid it.
Where I'm getting stuck is in doing this:
public delegate void NotifyDelegate<T>(IEnumerator<T> loadable, NotifyArgs na);
interface IObserver
{
void Notify<T>(IEnumerator<T> loadable, NotifyArgs na);
}
class Subject
{
NotifyDelegate notifier; //won't compile: needs type args
void Register(IObserver o)
{
notifier += o.Notify;
}
}
Of course, I could make the Subject generic as well, but then I have to have a separate Subject for each type. Does anyone have any advice here? Is there some piece of functionality that I'm missing somewhere or am I overcomplicating this?
UPDATE: I did oversimplify the arguments that Notify and NotifyDelegate take. Instead of this:
public delegate void NotifyDelegate<T>(NotifyArgs na);
I'm actually wanting to do something like this:
public delegate void NotifyDelegate<T>(IEnumerator<T> loadable, NotifyArgs na);
What I'm basically trying to pass back and forth is data from a database. Sorry if the previous code sample confused anyone.