Read return value from delegate

I am not sure if I understood the use of delegates correctly, but I would like to read the value of the returned delegate in the publisher class. Below is an example with a description.

//Publisher class public class ValidateAbuse { public delegate List<String> GetAbuseList(); public static GetAbuseList Callback; public void Ip(string ip) { // I would like to read GetAbuseList value (List<String>) here. How to do that? } } //Subscriber class class Server { public static void Start() { ValidateAbuse.Callback = GetIpAbuseList; ValidateAbuse.Ip(MyIp); } private static List<string> GetIpAbuseList() { //return List<String> to ValidateAbuse class and use return value in public void Ip(string ip) method } 
+6
source share
5 answers
 public void Ip(string ip) { if (Callback != null) { List<String> valueReturnedByCallback = Callback(); } } 
+6
source

It should be simple:

 // Ip in your code sample is missing static public static void Ip(string ip) { List<string> abuseList; if (Callback != null) abuseList = Callback() } 

However, you can avoid creating a delegate together using Func :

 public static Func<List<string>> Callback; 
0
source

Here is a version that does not use static for ValidateAbuse and uses the built-in delegate Func<T> .

 public class ValidateAbuse { private Func<List<string>> callback; public ValidateAbuse(Func<List<string>> callback) { this.callback = callback; } public void Ip(string ip) { var result = callback(); } } public class Server { public static void Start() { var validateAbuse = new ValidateAbuse(GetIpAbuseList); validateAbuse.Ip(MyIp); } private static List<string> GetIpAbuseList() { //return List<string> to ValidateAbuse class and use return value in public void Ip(string ip) method } } 

I recommend that you avoid static , as this gives you a global state that can lead to connection problems later on and also makes unit test difficult for you.

The other answers provided so far have a guard suggestion checking callback for null. If this is not the expected behavior (that Callback is null), I would avoid this. Better to collapse first than hard to debug errors later.

I would also try to make the server non-stationary.

0
source

Try the following: Read more here http://msdn.microsoft.com/en-us/library/bb534960%28v=vs.110%29.aspx

 internal delegate int PowerOfTwo(); void Main(){ PowerOfTwo ch = new PowerOfTwo(CheckPower); Console.WriteLine(ch()); } int CheckPower(){ return 2*2; } 
0
source

@ 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!

0
source

Source: https://habr.com/ru/post/913317/


All Articles