I think I got a working version of your second idea. The trick was to use event accessors. The type of the value parameter in the Dictionary should be a list - I took a shortcut so it works only for one subscriber per type :)
<br /> class TEventArgs<T> : EventArgs { public T Value { get; set; } }<br /><br /> class Test<br /> {<br /> Dictionary<Type, object> d = new Dictionary<Type,object>();<br /><br /> public event EventHandler<TEventArgs<int>> MyEvent1<br /> {<br /> add<br /> {<br /> d.Add(typeof(int), value); <br /> }<br /><br /> remove<br /> {<br /> d.Remove(typeof(int));<br /> }<br /> }<br /><br /> public event EventHandler<TEventArgs<string>> MyEvent2<br /> {<br /> add<br /> {<br /> d.Add(typeof(string), value);<br /> }<br /><br /> remove<br /> {<br /> d.Remove(typeof(string));<br /> }<br /> }<br /><br /> public void OnNotify<T>(T t)<br /> {<br /> EventHandler<TEventArgs<T>> del = (EventHandler<TEventArgs<T>> ) d[t.GetType()];<br /> del(this, new TEventArgs<T>() { Value = t });<br /> }<br /> }<br /><br /> static void Main()<br /> {<br /> Test t = new Test();<br /> t.MyEvent1 += new EventHandler<TEventArgs<int>>((sender, args) => Console.WriteLine(args.Value + 1));<br /> t.MyEvent2 += new EventHandler<TEventArgs<string>>((sender, args) => Console.WriteLine(args.Value.Trim()));<br /><br /> t.OnNotify(23);
The code inside the
add
and
remove
accessors is very similar, but the compiler doesn't like if the event declaration has a generic parameter.