@ Torbjörn Kalin's answer is good, but only if you only have 1 delegate from which you want to get the return value. If you want to get the return values of more than one delegate, here is how you do it:
//Publisher class public class ValidateAbuse { public delegate List<String> GetAbuseList(); public static GetAbuseList Callback; public void Ip(string ip) { foreach (GetAbuseList gal in Callback.GetInvocationList()) { List<string> result = gal.Invoke(/*any arguments to the parameters go here*/); //Do any processing on the result here } } } //Subscriber class class Server { public static void Start() { //Use += to add to the delegate list ValidateAbuse.Callback += GetIpAbuseList; ValidateAbuse.Ip(MyIp); } private static List<string> GetIpAbuseList() { //return code goes here return new List<String>(); }
This will call each delegate one by one, and you can handle the output of each delegate separately from each other.
The key here is the +=
operator (not the =
operator) and iterates through the list, which is retrieved by calling GetInvocationList()
, and then invokes Invoke()
for each delegate received.
I realized this after reading this page: https://www.safaribooksonline.com/library/view/c-cookbook/0596003390/ch07s02.html (although this was partly because I already had an idea what to do and I did not start a free trial to read the rest)
Hope this helps!
source share