如何在 C# 中对 Dictionary> 使用 Delegate

发布于 2024-08-30 14:44:49 字数 972 浏览 2 评论 0 原文

代码:

[1]

private delegate void ThreadStatusCallback(ReceiveMessageAction action, 
      Dictionary<int, List<string>> message);

[2]

Dictionary<int, List<string>> messagesForNotification = 
      new Dictionary<int, List<string>>();

[3]

Invoke(new ThreadStatusCallback(ReceivesMessagesStatus), 
     ReceiveMessageAction.Notification , messagesForNotification );

[4]

private void ReceivesMessagesStatus(ReceiveMessageAction action, object value)
{
    ...
}

如何分别发送ReceiveMessageAction类型的两个变量Dictionary>到 ReceivesMessagesStatus 方法。

谢谢。


我有一个在线程中执行的函数。在这里,我创建了一个 Dictionary > 类型的对象,并使用委托,我想在我的表单中使用该对象。

部分代码如上: [1] 受托人宣誓 [2] 对象 [3] 代表的召唤 [3] 需要发送对象的函数

The code:

[1]

private delegate void ThreadStatusCallback(ReceiveMessageAction action, 
      Dictionary<int, List<string>> message);

[2]

Dictionary<int, List<string>> messagesForNotification = 
      new Dictionary<int, List<string>>();

[3]

Invoke(new ThreadStatusCallback(ReceivesMessagesStatus), 
     ReceiveMessageAction.Notification , messagesForNotification );

[4]

private void ReceivesMessagesStatus(ReceiveMessageAction action, object value)
{
    ...
}

How can I send the two variable of type ReceiveMessageAction respectively Dictionary<int, List<string>> to the ReceivesMessagesStatus method.

Thanks.


I have a function that is executed in a thread. Here I create an object of type Dictionary <int, List <string>> and using a delegation I whant to use this object in my form.

A part of the code is above:
[1] Decraration of the delegated
[2] The object
[3] The Call Of the delegate
[3] the function Where the object Need To Be send

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

御守 2024-09-06 14:44:49

您可以通过使用 lambda 来实现类型安全,而无需进行强制转换:

  Invoke(new MethodInvoker(
    () => ReceivesMessagesStatus(ReceiveMessageAction.Notification, messagesForNotification)
  ));

You can do it type-safe without casting by using a lambda:

  Invoke(new MethodInvoker(
    () => ReceivesMessagesStatus(ReceiveMessageAction.Notification, messagesForNotification)
  ));
眼睛会笑 2024-09-06 14:44:49

您是否尝试过将 value 转换为字典?例如

private void ReceiveMessagesStatus(ReceieveMessageAction action, object value)
{
    var myDictionary = (Dictionary<int, List<string>>)value;
    ...
}

Have you tried casting value as a Dictionary? e.g.

private void ReceiveMessagesStatus(ReceieveMessageAction action, object value)
{
    var myDictionary = (Dictionary<int, List<string>>)value;
    ...
}
梦魇绽荼蘼 2024-09-06 14:44:49

解决方案:

[1]

Invoke(new ThreadStatusCallback(ReceivesMessagesStatus), new object[] { ReceiveMessageAction.Notification, messagesForNotification } );

[2]

private void ReceivesMessagesStatus(ReceiveMessageAction action, Dictionary<int, List<string>> value)
{
    ...
}

Solution:

[1]

Invoke(new ThreadStatusCallback(ReceivesMessagesStatus), new object[] { ReceiveMessageAction.Notification, messagesForNotification } );

[2]

private void ReceivesMessagesStatus(ReceiveMessageAction action, Dictionary<int, List<string>> value)
{
    ...
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文