如何在C#中使用Delegate for Dictionary <int,list <string =“”>&gt; </int,>

时间:2010-04-24 09:05:58

标签: c# list dictionary delegates invoke

代码:

[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<int, List<string>>分别发送到ReceivesMessagesStatus方法。

感谢。


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

部分代码如上: [1]委托的裁减 [2]对象 [3]代表的召唤 [3]对象需要发送的功能

3 个答案:

答案 0 :(得分:2)

您可以使用lambda:

进行类型安全而无需进行强制转换
  Invoke(new MethodInvoker(
    () => ReceivesMessagesStatus(ReceiveMessageAction.Notification, messagesForNotification)
  ));

答案 1 :(得分:1)

您是否尝试过将值转换为字典? e.g。

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

答案 2 :(得分:1)

解决方案:

[1]

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

[2]

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