繁体   English   中英

如何在C#中为字典使用委托 <int, List<string> &gt;

[英]How to use Delegate in C# for Dictionary<int, List<string>>

编码:

[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)
{
    ...
}

如何将类型分别为Dictionary<int, List<string>> ReceiveMessageAction类型的两个变量发送到ReceivesMessagesStatus方法。

谢谢。


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

上面的代码部分:[1]委托的dedecration [2]对象[3]委托的调用[3]函数需要将对象发送到的功能

您可以使用lambda进行类型安全而无需强制转换:

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

您是否尝试过将价值铸成词典? 例如

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

解:

[1]

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

[2]

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM