можно ли сделать что-то подобное ? не найду синтаксис, имеет смысл в следующем примере:
namespace Test
{
using System.Collections.Generic;
public class GenericsTest
{
public interface ITopic<E> {}
public delegate void Handler<E>(E e);
public interface ISubscriber<E>
{
void Handle(E e);
}
/// <summary>
/// signatures are parameterized to ensure the compatibility
/// of topic and handler: handler is capable to process
/// the events published on topic
/// </summary>
public interface ISubManager
{
void Subscribe<E>(ITopic<E> topic, Handler<E> handler);
void Unsubscribe<E>(ITopic<E> topic, Handler<E> handler);
}
public class TopicS : ITopic<string> {}
public class TopicI : ITopic<int> {}
public class HandlerS : ISubscriber<string>
{
public void Handle(string s) { }
}
public class HandlerI : ISubscriber<int>
{
public void Handle(int i) { }
}
public class SubAdapter<E>
{
public ITopic<E> topic;
public ISubscriber<E> subscriber;
public SubAdapter(ITopic<E> topic, ISubscriber<E> subscriber)
{
this.topic = topic;
this.subscriber = subscriber;
}
}
static IList<object> listeners = new List<object>();
static ISubManager subManager;
public static void Main(string[] args)
{
}
static void Add<E>(ITopic<E> topic, ISubscriber<E> s)
{
listeners.Add(new SubAdapter<E>(topic, s));
subManager.Subscribe(topic, s.Handle);
}
static void Remove<E>(ITopic<E> topic, ISubscriber<E> s)
{
// remove from the list: listeners.Remove(s);
subManager.Unsubscribe(topic, s.Handle);
}
/// <summary>
/// problem is here !!!
/// </summary>
static void RemoveAll()
{
foreach (var adapter in listeners)
{
SubAdapter<? : object> sa = (SubAdapter<? : object>) adapter;
subManager.Unsubscribe(sa.topic, sa.subscriber.Handle);
}
listeners.Clear();
}
}
}