Getting ObjectModel.ReadOnlyCollection for foreach

725 Views Asked by At

I want to get the values and keys of the Object. First I cast it and then put it on the foreach loop to get keys and values.

object itemAttributes = web.javaScriptExecutor().ExecuteScript($"return window.INITIAL_STATE.items.itemAttributes")

                    var items = (ReadOnlyCollection<object>)itemAttributes;


                        foreach (Dictionary<string, object> item in items)
                        {
                            foreach (KeyValuePair<string, object> kvp in item)
                            {
                                if (Convert.ToString(kvp.Value) != null)
                                {
                                     Name = Convert.ToString(kvp.Key), Value = Convert.ToString(kvp.Value) });
                                }
                            }
                        }

I get an error when I want to cast

var items = (ReadOnlyCollection<object>)itemAttributes;

//Unable to cast object of type 'System.Collections.Generic.Dictionary`2[System.String,System.Object]' to type 'System.Collections.ObjectModel.ReadOnlyCollection`1[System.Object]'.
0

There are 0 best solutions below