簡體   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