jacknad
jacknad

Reputation: 13739

How to address co-variant array conversion?

I have an image stitching task that could take a lot of time so I run it as a seperate task like this

var result = openFileDialog.ShowDialog();
BeginInvoke(new Action<string[]>(StitchTask), openFileDialog.FileNames);

private void StitchTask(string[] fileNames)
{
    // this task could take a lot of time
}

Do I need to worry about the co-variant array conversion warning below or am I doing something wrong?

Co-variant array conversion from string[] to object[] can cause run-time exception on write operation

Upvotes: 7

Views: 4682

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1502236

Got it - the problem is that you're passing a string[] as if it were an array of arguments for the delegate, when you actually want it as a single argument:

BeginInvoke(new Action<string[]>(StitchTask),
            new object[] { openFileDialog.FileNames });

Whatever's giving you the warning is warning you about the implicit conversion of string[] to object[], which is reasonable because something taking an object[] parameter might try to write:

array[0] = new object();

In this case that isn't the problem... but the fact that it would try to map each string to a separate delegate parameter is a problem.

Upvotes: 13

Related Questions