I am building a system a bit like LINQ, while trying to maintain polymorphic callback handler lists, and I am facing several types of problems. A short way to ask your question is to simply show you the code. My new system supports βGroupsβ, and groups have a vector of entry points (below, UPDATE and CHECKPT), and each element of the vector is a polymorphic delegate list that I will call back using reflection.
So, an example code:
namespace ConsoleApplication1 { internal delegate void GroupISDHandler(int i, string s, double d); class Group { public class myHandlers { internal List<Delegate> hList = new List<Delegate>(); public static myHandlers operator +(myHandlers a, Delegate b) { a.hList.Add(b); return a; } } public class mimicVector { public List<myHandlers> ListofhLists = new List<myHandlers>(); public myHandlers this[int i] { get { return ListofhLists[i]; } set { ListofhLists[i] = value; } } } public mimicVector handlers = new mimicVector(); public Group(string name) { ... } } class Program { internal const int UPDATE = 0; internal const int CHECKPT = 1; public static void Main() { Group g = new Group("group name"); g.handlers[UPDATE] += (GroupISDHandler)delegate(int x, string s, double d) { Console.WriteLine("my int,string,double handler was called, with x = {0}, s = {1}, d = {2}", x,s,d); }; } } }
My questions are focused on the registration line. Why can't C # output types so that I can completely omit the class and the new delegate type? I would have thought that from
g.handlers[UPDATE] += delegate(int x, string s, double d) { Console.WriteLine(....); };
C # can output the required type signature. delegate () will be a kind of anonymous type, and C # will just generate something like
private delegate void _atype1(int _a0, string _a1, double _a2)
and then insert (Delegate)(_atype1) before compiling the string. This way, my user will not need to declare a delegate type (which currently forces her to enter a list of arguments twice).
I have System.Linq since I am on VS 2010. Therefore, if LINQ can somehow bring out the necessary casts ...