C #: less ugly syntax for creating delegate lists?

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 ...

+4
source share
2 answers

It turns out that the answer is basically this: although you can conclude in those situations that I had in mind, C # owners want completely general solutions, and polymorphism makes the type inference problem for the solution too difficult in a fairly general way, in their opinion. I myself do not agree that in the end I print all my type signatures, but this is their reasoning.

0
source

you should do it like this:

g.handlers[UPDATE] += (GroupISDHandler)((x, s, d) => Console.WriteLine( "my int,string,double handler was called, with x = {0}, s = {1}, d = {2}", x, s, d));

another variant:

has a class called "Parameter", which is a container for anyone the user can be sent to, can be certain types if they never change, or a list of objects if you pretend to send and receive a different number of parameters. Then, instead of the Delegate, you should accept an Action equal to the delegate that takes one argument, and you could make your call without casting as follows:

p => Console.WriteLine ("x = {0}, s = {1}, d = {2}", px, ps, pd);

+2
source

All Articles