开发者

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

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 c开发者_StackOverflow中文版reate 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


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

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


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

private void ReceiveMessagesStatus(ReceieveMessageAction action, object value)
{
    var myDictionary = (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)
{
    ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜