IDictionary

I’m working on some code to save all objects in my scene to. I want to save each item as an IDictionary type and then save them all in an iList. The reason I’m using these types are because I will be saving the level to Parse.com and they only allow certain data types.

In the code below it is working for the most part until I try to iterate over the Dictionaries in the List. Because I’m declaring them as objects I cant access the Dictionary. I get the following error:
error CS1579: foreach statement cannot operate on variables of type object' because it does not contain a definition for GetEnumerator’ or is not accessible

I’m not that familiar with C#, or IDictionary and List types. I’m thinking that declaring the Dictionaries instead of objects would be the solution but it breaks my code if I do that.

IEnumerable<ParseObject> results;
IList<object> objectsList = new List<object>{};
	


void GetAllObjects () 
{
	
	var blocks = GameObject.FindGameObjectsWithTag ("GameItems");
	foreach (var obj in blocks) 
	{
		IDictionary<string, object> levelBlocks = new Dictionary<string, object>{};
		levelBlocks.Add("name", obj.name);
		levelBlocks.Add("x", obj.transform.position.x);
		levelBlocks.Add("y", obj.transform.position.y);
		levelBlocks.Add("rotation", obj.transform.eulerAngles.z);
		
		objectsList.Add(levelBlocks);
		
	}
	
	
	ParseObject myObject = new ParseObject("MyObject");
	myObject["oneLevel"] = objectsList;
	myObject.SaveAsync();
	
}

void DestroyAllObjects()
{
	var getAllObjects = GameObject.FindGameObjectsWithTag ("GameItems");
	foreach (object obj in getAllObjects) 
	{
		GameObject objCurrent = (GameObject)obj;
		Destroy (objCurrent);
	}
}

void MakeAllObjects()
{
	foreach (object obj in objectsList) 
	{
		Debug.Log (obj);
		foreach (KeyValuePair<string, object> pair in obj)
		{
			Debug.Log (pair.Key + " " + pair.Value);
		}
	}		
}

void Start () 
{
	GetAllObjects ();
	DestroyAllObjects ();
	MakeAllObjects();
}

I don’t know why it is breaking your code - it should be perfectly ok to use IList of IDictionary:

IList<IDictionary<string, object>> objectsList = new List<IDictionary<string, object>>();

and then iterate using:

foreach (var dic in objectsList) 
{
    foreach (var pair in dic)
    {
        // ...
    }
}