Results 1 to 4 of 4

Thread: Generic delegate in C#

  1. #1
    Join Date
    Jun 2009
    Posts
    3,960

    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();
                }
            }
        }
    }
    TestDelegate :

    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();
            }
        }
    }
    The main test:

    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);
            }
        }
    }
    The problem is as follows:

    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. #2
    Join Date
    Nov 2008
    Posts
    1,192

    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. #3
    Join Date
    May 2008
    Posts
    685

    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();
    }
    It can be transformed into this :

    Code:
    if (action is Callback2)
    {
        ((Callback2)action).Invoke(x, y);
    }
    else if(action is Callback)
    {
        ((Callback)action).Invoke();
    }
    If passed, as explained in another topic, you can free yourself writing multiple vendors. You can assign variables without constructors, like this:

    Code:
    GenericCallbacks instance = new GenericCallbacks()
    {
        x = 1,
        y= 2
    };
    In this case, it is necessary that the variables x and y are public.

    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. #4
    Join Date
    Jun 2009
    Posts
    3,960

    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);
    "EmptyCallback" and type void (), but I have several different delegate.
    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)

Similar Threads

  1. Replies: 5
    Last Post: 20-03-2010, 02:32 AM
  2. Delegate Control of OU in AD 2008
    By Damien25 in forum Active Directory
    Replies: 1
    Last Post: 05-02-2010, 06:32 PM
  3. where to look for delegate in Active Directorey
    By Gaurav Bhardwaj in forum Active Directory
    Replies: 8
    Last Post: 13-06-2009, 03:45 AM
  4. Delegate permission let one user to join pc to a domain
    By Bhuvan in forum Active Directory
    Replies: 3
    Last Post: 19-08-2008, 02:21 AM
  5. Delegate exchange tasks
    By Manik in forum Active Directory
    Replies: 4
    Last Post: 18-03-2007, 10:36 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,066,922.25860 seconds with 16 queries