The code below shows a generic class with a type constraint (Pub
). The class has an event that it can raise allowing us to pass a message to subscriber
Replace IMsg with T
public interface IMsg { } // Doesn't work
public class Msg : IMsg { }
public class Pub where T : IMsg
{
public event Action notify;
public void Subscribe(object subscriber)
{
IHandler implementer = subscriber as IHandler; // here
if (implementer != null)
{
this.notify += implementer.NotifyEventHandler;
}
}
}
public interface IHandler where T : IMsg
{
void NotifyEventHandler(T data);
}