Exception bei Invoke mit Parameter String-Array und List<string>

  • C#
  • .NET (FX) 4.5–4.8

    Exception bei Invoke mit Parameter String-Array und List<string>

    Moin Leute.

    Ich möchte per (Begin)Invoke einen Parameter string[] bzw. List<string> an eine entsprechende Prozedur übergeben:
    Spoiler anzeigen

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Windows.Forms;
    4. // Form mit 3 Button
    5. namespace WindowsFormsApp1
    6. {
    7. public partial class MainForm : Form
    8. {
    9. // TODO: Ggf. den Pfad Anpassen
    10. private const string TempDir = "D:\\Temp\\";
    11. public MainForm()
    12. {
    13. this.InitializeComponent();
    14. }
    15. private void Button1_Click(object sender, EventArgs e)
    16. {
    17. string[] files = System.IO.Directory.GetFiles(TempDir);
    18. Action<string[]> action = new Action<string[]>(this.Test);
    19. this.Invoke(action, files);
    20. //this.BeginInvoke(action, files);
    21. }
    22. private void Button2_Click(object sender, EventArgs e)
    23. {
    24. string[] files = System.IO.Directory.GetFiles(TempDir);
    25. List<string> ll = new List<string>(files);
    26. Action<List<string>> action = new Action<List<string>>(this.Test2);
    27. this.Invoke(action, files);
    28. //this.BeginInvoke(action, files);
    29. }
    30. private void Button3_Click(object sender, EventArgs e)
    31. {
    32. Action<string> action = new Action<string>(this.Test3);
    33. this.BeginInvoke(action, "meldung");
    34. }
    35. private void Test(string[] files)
    36. {
    37. MessageBox.Show(files.Length.ToString());
    38. }
    39. public void Test2(List<string> files)
    40. {
    41. MessageBox.Show(files.Count.ToString());
    42. }
    43. public void Test3(string meldung)
    44. {
    45. MessageBox.Show(meldung);
    46. }
    47. }
    48. }
    Während der Laufzeit kommt bei string[] und List<string> folgende Exception:


    Hat jemand von Euch eine Lösung dazu?

    Danke.

    ================================================================
    Geklärt.
    In Button2_Click() habe ich fälschlicherweise ein Array übergeben, während eine List<> empfangen wird, hier hat wohl der Compiler eine automatische Konvertierung vorgenommen.
    Also - dies funktioniert:
    Spoiler anzeigen

    C#-Quellcode

    1. using System;
    2. using System.Collections.Generic;
    3. using System.Windows.Forms;
    4. namespace WindowsFormsApp1
    5. {
    6. public partial class MainForm : Form
    7. {
    8. // TODO: Ggf. den Pfad Anpassen
    9. private const string TempDir = "D:\\Temp\\";
    10. public MainForm()
    11. {
    12. this.InitializeComponent();
    13. }
    14. private void Button2_Click(object sender, EventArgs e)
    15. {
    16. string[] files = System.IO.Directory.GetFiles(TempDir);
    17. List<string> ll = new List<string>(files);
    18. Action<List<string>> action = new Action<List<string>>(this.Test2);
    19. //this.Invoke(action, ll);
    20. this.BeginInvoke(action, files);
    21. }
    22. public void Test2(List<string> files)
    23. {
    24. MessageBox.Show(files.Count.ToString());
    25. }
    26. }
    27. }
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „RodFromGermany“ ()