|
|
![]() |
| Thread Tools | Search this Thread |
#1
| |||
| |||
Generic delegate in C# I have a small concern about generic delegates in C#. Here's the context: I created 2 classes: - GenericCallbacks: By default a method pointer and the call when asked. - TestDelegate: It creates GenericCallbacks and methods of test attached to delegates. Here is the complete code compiled, I will issue the following: GenericCallbacks: Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestGenericDelegate { class GenericCallbacks { private Delegate action; private int x; private int y; public delegate void Callback(); public delegate void Callback2(int x, int y); /* * [...] * Many delegate prototyping different * Not very clean.. */ public GenericCallbacks(Delegate Action) { this.action = Action; this.x = this.y = 0; } // public GenericCallbacks(Callback2 Action) // { // this.action = Action; // this.x = this.y = 5; // } public void Invoke() { /* After various operations on x and y means I know what cast */ if (this.x != 0 && this.y != 0) { ((Callback2)action).Invoke(x, y); } else { ((Callback)action).Invoke(); } } } } Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestGenericDelegate { class TestDelegate { public void Method1() { Console.WriteLine("Method 1." ); return; } public void Method2(int x, int y) { Console.WriteLine("Method 2 : {0}, {1}.", x, y); return; } public TestDelegate() { GenericCallbacks unitTest = new GenericCallbacks(Method1); GenericCallbacks unitTest2 = new GenericCallbacks(Method2); unitTest.Invoke(); unitTest2.Invoke(); } } } Code: using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace TestGenericDelegate { class Program { static void Main(string[] args) { TestDelegate testDelegate = new TestDelegate(); Console.ReadKey(true); } } } How to avoid creating so many constructors of delegate type in the class GenericCallbacks? In the example I only have 2, but in the real program that asks me worry I have about 10, knowing that I have 1 overload for each type of delegates which gives 20 overload for the same how to add! In my case, even if it's ugly, it might work, but I dare not imagine that the developer is left with 250 possible prototype for its delegates. I tried to make the constructor a type System.Delegate but it is not working. I'm looking for a generic type that can contain a pointer to method but I found none. |
#2
| |||
| |||
Re: Generic delegate in C# It has been a long time since I have not touched C#, but I really find it difficult to understand in which case you might want to abstract the prototype of your delegates |
#3
| |||
| |||
Re: Generic delegate in C# I do not understand what you're looking for and what you need may be simpler also. But then also a small remark on your example: Code: if (this.x != 0 && this.y != 0) { ((Callback2)action).Invoke(x, y); } else { ((Callback)action).Invoke(); } Code: if (action is Callback2) { ((Callback2)action).Invoke(x, y); } else if(action is Callback) { ((Callback)action).Invoke(); } Code: GenericCallbacks instance = new GenericCallbacks() { x = 1, y= 2 }; Another possibility: use the keyword params to specify a constructor (or method) can have any number of parameter type. Ex: Code: public class myClass { public myClass(params int[] list) { int a = list[0]; } } myClass myClass1 = new myClass(1, 2, 3, 4); |
#4
| |||
| |||
Re: Generic delegate in C# Thank you for your reply, I did not know these techniques very useful. In contrast, what is the purpose of the manipulation is simply to avoid declaring delegate method 1. In my case it was for the addition of a control, and I find myself using methods like: Code: public void AddControl(string Name, Keys Keys, EmptyCallback Action, bool KeyRepeat); I have also overload of this method (AddControl) to take a picture of Keys. So I find myself with 2 methods AddControl type of delegate. With 10 types of delegate instance, you can imagine the carnage ... I was simply trying to clarify the code because I found it very dirty. Otherwise the technique by fellah statement is interesting, but the fact of putting its attributes in public is not contrary to the concept object? (encapsulating all that) |
![]() |
|
Tags: csharp, delegate |
Thread Tools | Search this Thread |
|
![]() | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
Issues in Outlook 2007 while trying to delegate information | jhon | Windows Software | 5 | 20-03-2010 02:32 AM |
Delegate Control of OU in AD 2008 | Damien25 | Active Directory | 1 | 05-02-2010 06:32 PM |
where to look for delegate in Active Directorey | Gaurav Bhardwaj | Active Directory | 8 | 13-06-2009 03:45 AM |
Delegate permission let one user to join pc to a domain | Bhuvan | Active Directory | 3 | 19-08-2008 02:21 AM |
Delegate exchange tasks | Manik | Active Directory | 4 | 18-03-2007 10:36 AM |