- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想通过调用多播委托(delegate)来组合两个函数调用返回的结果。但是我不断收到一个异常,说 del 是一个变量,但像方法一样使用。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace MultiDelegateConsoleApplication
{
public delegate void SampleMultiDelegate(string args,out string SampleString);
class Program
{
public static void SayHello(string args,out string s1)
{
s1 = "Hello " + args;
}
public static void SayGoodbye(string args,out string s2)
{
s2 = "Goodbye " + args;
}
static void Main(string[] args)
{
SampleMultiDelegate sampleMultiDelegate = new SampleMultiDelegate(SayHello);
sampleMultiDelegate += SayGoodbye;
string param1 = "Chiranjib";
string param2,param3;
Console.WriteLine("**************Individual Function Invoke***********");
SayHello(param1,out param2);
SayGoodbye(param1, out param3);
Console.WriteLine("**************Multicast Delegate Invoke***********");
sampleMultiDelegate(param1,out param2);
Console.WriteLine(param2); //The multicast delegate will always return the result of the last function
string result;
foreach (Delegate del in sampleMultiDelegate.GetInvocationList())
{
result = del(param1,out param2);
}
Console.ReadKey();
Console.ReadLine();
}
}
}
你能解释一下并帮助我修复错误吗?
最佳答案
您需要将调用列表中的每个函数都转换为委托(delegate)类型才能使用正常的函数调用语法:
void Main()
{
var sampleMultiDelegate = new SampleMultiDelegate(SayHello);
sampleMultiDelegate += SayGoodbye;
var param1 = "Chiranjib";
string param2;
string result = "";
foreach (var del in sampleMultiDelegate.GetInvocationList())
{
var f = (SampleMultiDelegate)del;
f(param1, out param2);
result += param2 + "\r\n";
}
Console.WriteLine(result);
}
还修复了这样一个事实,即您的委托(delegate)调用在返回 void
时不会有任何结果。
关于c# - 如何在 C# 中合并 Multicast Delegates 返回的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31636702/
我想调试一些多播问题,我希望有一些小程序/实用程序来显示传入的多播数据包。 从发送机器(A),我使用理查德史蒂文斯的 sock发送多播数据包(源端口=目标端口=7000)的程序(随他的TCP/IP I
我正在开发一个由多个模块组成的应用程序,并要求它们相互共享信息。示例:发布/订阅 模块发布一些信息(例如状态变量)并且对特定信息感兴趣的模块获取它的场景。或 请求/回复 感兴趣的模块明确询问信息并得到
我正在实现一个示例,其中服务器监听网络中的任何 Activity 客户端。 我使用数据报套接字让服务器进行多播,并使用客户端来响应服务器。 public void run() { try {
我想了解在多播委托(delegate)中处理异常的最佳方式是什么? 我的问题是,如果方法在多播委托(delegate)执行中抛出异常会怎样?它会停止吗?继续 ?怎么处理 ?如果有人可以分享,一个带解释
我仍在尝试理解 DDS 及其概念。 我有一个配置,其中两台笔记本电脑运行基于 dds 的应用程序。我的环境不允许多播,因此我决定采用点对点连接(单播)。为了将两台笔记本电脑置于同一网络中,我使用以太网
可以联系async生成器以某种方式广播或多播,以便其所有迭代器(“消费者”?订阅者?)接收所有值? 考虑这个例子: const fetchMock = () => "Example. Imagine
我的网络上有多个服务器,它们都发送广播消息。使用以下客户端,我 try catch 来自所有服务器的所有广播消息。发送部分工作正常(不包含在本文中),但我的接收部分不起作用...我不断收到“Socke
我刚开始接触网络,正在编写一个非常简单的多播代码。我仍然不确定不同的接口(interface)。一些示例使用“0.0.0.0”,而其他示例使用“127.0.0.1”。 服务器代码 import soc
我正在使用一个由大约 40 个节点组成的集群运行 Debian 4。每个节点都运行一个守护进程,该守护进程位于并监听多播 IP。 我编写了一些客户端软件,通过 LAN 发送多播,客户端计算机与集群位于
我正在尝试创建一个 SOAP 客户端。但不断收到此错误。有什么想法吗?我尝试更改IP,但仍然出现同样的错误。 MulticastSocket.Java public void joinGroup(
我在 https://developer.apple.com/documentation/combine/publisher 中的“与多个订阅者合作”部分遇到了一些合并问题。 : func multi
我已经完成了一个示例 ZeroMQ PGM 多播应用程序,它运行良好。 但是回复处理不起作用。 这种方法是否正确? 如果是 - 如何从接收方回复发送方? 发件人: std::string msg =
我尝试将 Multi-Casting 企业集成模式 (EIP) 与 Apache Camel 结合使用,但遇到了很多运行时异常。我认为主要错误如下。如果有人可以帮我解决这个问题,我想我可能可以运行这个
我将服务器和客户端都编写为单独的应用程序。他们通过 UDP 多播进行通信(因为我需要加入该组的每个人都可以读取和写入消息)。现在我有两个窗口,但我的目标是创建一个简单的聊天程序,但我不知道如何同时收听
如果委托(delegate)是为对象之间的一对一关系设计的,而 NSNotifications 是为一对多的潜在关系设计的,那么是否有针对一对多的最佳实践? 我在 iOS 中看到过很多自定义多播委托(
我在 TP-LINK TL-WR703N 上使用 OpenWRT。我需要阻止有线端的大量组播数据在 Wifi 上传输。我想我可以为此使用 IGMP 监听,因为我发现了一条评论 "starting fr
我已经学习 RxJs 几个星期了,我似乎无法理解 Subjects 和 Multicasting an observable 之间到底有什么区别。 我找到了许多区分可观察对象和主题的来源,但我找不到任
亲爱的社区。 我尝试以不同的方式在 spring config xml 中添加多播聚合策略,这是我在 apache 论坛、stackoverflow 和许多其他从 java dsl 转换而来的地方找到
谁能给我一个java中的例子来从DatagramSocket接收数据并通过Multicast Socket发送相同的数据 最佳答案 发送多播数据报 为了在 Java 中发送任何类型的数据报,无论是单播
我正在使用 MulticastSocket,每当我尝试加入一个组时,当我在“localhost”ip 上运行该组时,它永远不会工作。然而,我找到了这篇文章http://lycog.com/progra
我是一名优秀的程序员,十分优秀!