mardi 10 septembre 2019

How to sort through a generic list object

My code is designed to parse an unknown json file using json.net that has nested classes and create checkboxes to easily look through it.

At some point during the parsing it arrives to a List object

I have the type and the actual object

The type has AssemblyQualifiedName "System.Collections.Generic.List`1[[Calibration.Camera, CalibrationOrganizer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"

The object is a List<Calibration.Camera>

If I loop through the List this way it works, but the problem is I'm assuming a known data type, which I don't. This is just one of many data types in the json file

if (currentType.Name == "List`1")
            {
                for (int i = 0; i < ((List<Calibration.Camera>)currentValue).Count; i++)
                {
                    cbox = new CheckBox();
                    cbox.Text = "[" + i.ToString() + "]";
                    cbox.Name = prev_properties + "!" + curr_property;
                    cbox.AutoSize = true;
                    cbox.Location = new Point(1100 + prop_list.Count() * 100, i++ * 20); //vertical
                    cbox.CheckedChanged += new EventHandler(ck_CheckedChanged);
                    this.Controls.Add(cbox);
                }
            }

If I try this, it wont compile

for (int i = 0; i < currentValue.Count; i++) {...}

with error: Operator '<' cannot be applied to operands of type 'int' and 'method group'

If I try this, it crashes

for (int i = 0; i < ((List<object>)currentValue).Count; i++)

with exception: System.InvalidCastException: 'Unable to cast object of type 'System.Collections.Generic.List1[Calibration.Camera]' to type 'System.Collections.Generic.List1[System.Object]'.'

So Im not sure what I can do,

I can parse the AssemblyQualifiedName and get the Object type as a string, but how do I convert it to an object type again?





Aucun commentaire:

Enregistrer un commentaire