简体   繁体   中英

c# <dynamic> casting or conversion

I'm doing some work with an NI ADC. I'm currently reading in voltages from the AO and AI into List<dynamic> and I'm having some issues with console.writeLine.

I'm using the dynamic type because the program needs to decide what the data should be stored at, at runtime instead of compile time.

So, because of this whenever I want to print the contents of the list, it doesn't know what i'm asking so it returns the type being stored, not the selected element data.

public void createTask(DataGrid grid, List<Object> data, float sampleRate, int sampleAmount, ComboBox channel, float minRange, float maxRange)
        {
            using (Task myTask = new Task())
            {
                myTask.AIChannels.CreateVoltageChannel(channel.Text, "",
                    (AITerminalConfiguration)(-1), minRange, maxRange, AIVoltageUnits.Volts); // create the task to measure volts

                myTask.Timing.ConfigureSampleClock("", sampleRate, SampleClockActiveEdge.Rising, // create the timing
                    SampleQuantityMode.ContinuousSamples, sampleAmount);

                AnalogMultiChannelReader reader = new AnalogMultiChannelReader(myTask.Stream);

                myTask.Control(TaskAction.Verify);

                data.Add(reader.ReadSingleSample());

                Console.WriteLine(data[0]);
            }
        }

Which in turn prints out System.Double[] . How would I go about printing out what the element actually stores rather then its type? I've tried lots of different ways of trying to get what I'm after but I'm struggling with C# syntax (I use C++) - only been using it for three weeks.

I've tried;

  • Casting
  • ToString() Conversion
  • Copying the contents of the whole list to a <double> list with CopyTo.

I'm at a bit of a loss here.

1)You can iterate over the array

 double[] data = new double[] { 1, 2, 3 };
 foreach (var item in data)
       Console.WriteLine(item.ToString());

2)Or

 Console.WriteLine(string.Join(",", data)); 

The above solutions work if you already have an array of double. In your case ,if you are sure that data[0] is an array of double you can use the as operator

double[] temp = data[0] as double[];
Console.WriteLine(string.Join(",", temp));

Your List<object> data must be not empty before you call your method. If you do Console.WriteLine(data.Last()); instead of Console.WriteLine(data[0]) , you will surely get the result

So the first item in your list in an array of double. If you want to see the values from that array printed out, you can manually iterate through them, or you can use the following code to print out array items if the object is an array, otherwise just print the item value.

        if (data[0].GetType().IsArray)
        {
            Console.WriteLine(string.Join(",", ((Array)data[0]).Cast<object>()));
        }
        else
        {
            Console.WriteLine(data[0]);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM