Lets Learn

Opinion Matters

Question 6: Controlling Delegates and Multicast Delegate

Posted by Ankush on February 23, 2011


You have created a multicast delegate. When you invoke it, each delegate gets fired. You need want more control on this. Basically you want a fine control over:

a. order in which each delegate is invoked

b. firing only a subset of delegates

c. firing each delegate based on the success or failure of previous delegates

How will you achieve this?

Hint:
A delegate, when called, will invoke all delegatess to read within its invocation list. These delegates are usually invoked sequentially from the first to the last one added.With the use of the GetInvocationList method of the MulticastDelegate class, you can obtain each delegate in the invocation list of a multicast delegate. This method accepts no parameters and returns an array of Delegate objects that corresponds to the invocation list of the delegate on which this method was called.

Now can anybody offer the code solution:::)

Advertisements

2 Responses to “Question 6: Controlling Delegates and Multicast Delegate”

  1. AJ said

    This has to something like following:

    public delegate void testDelegate();
    static void Main(string[] args)
    {
    testDelegate t = new testDelegate(test1);
    t += new testDelegate(test2);
    t += new testDelegate(test3);
    t += new testDelegate(test4);
    t.Invoke();

    //now
    Delegate [] delArray = t.GetInvocationList();
    foreach(Delegate d in delArray)
    {
    testDelegate td = ((testDelegate)d);
    MethodInfo mi = td.Method;
    if(mi.Name.Contains(“1”) || mi.Name.Contains(“4”))
    ((testDelegate)d).Invoke();
    }

    Console.ReadKey();
    }

  2. AJ said

    here is the rest of the code:

    public static void test1()
    {
    Console.WriteLine(“test1 function”);
    }

    public static void test2()
    {
    Console.WriteLine(“test2 function”);
    }
    public static void test3()
    {
    Console.WriteLine(“test3 function”);
    }
    public static void test4()
    {
    Console.WriteLine(“test4 function”);
    }

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: