I have an Action delegate declared like this.
private readonly Action<List<PSDPoint>[]> _psdAction;
_psdAction = AddPSDData;
PSDPoint is a very simple class.
public class PSDPoint
{
public int Frequency { get; set; }
public double Power { get; set; }
}
I am attempting to call the delegate like this.
private void PSDData(object sender, PSDEventArgs e)
{
Dispatcher.Invoke(_psdAction, e.Data);
}
PSDEventArgs looks like this. You can see e.Data is the same data type.
public class PSDEventArgs
{
public List<PSDPoint>[] Data { get; set; }
public string[] Channels { get; set; }
}
_psdAction points to this function. I never reach this code.
private void AddPSDData(List<PSDPoint>[] data)
{
...
}
I get this error the dispatcher is attempting to invoke. I can’t figure out why. I’m passing in the right type. I’m guessing it’s something “funky” with having an array of Lists?
Object of type 'System.Collections.Generic.List`1[DataLogger.Model.PSDPoint]' cannot be converted to type 'System.Collections.Generic.List`1[DataLogger.Model.PSDPoint][]'.
Try the following
The problem is that the second parameter of
Invoketakes aparams object[]. The intent of this is to support delegates with variable number of arguments. The contents of the array are mapped to parameters. First element going to the first parameter, second element to the second parameter and so on.When you call it with
e.Datathe C# compiler sees an array. Instead of passing the array as the first argument it tries to pass each element as a parameter. By casting toobjectyou can force the compiler to interpret it as a single parameter