You really don't need to collect the keys and repeat them again if you iterate over the dictionary in the reverse order (from "menu.Count - 1" to zero). Iterating in the direct order, of course, will result in mutated collection exceptions if you start deleting things.
, ActionDictionary, , , Dictionary<string,object>.
static int counter = 0;
private static void RemoveNotPermittedItems(Dictionary<string, object> menu)
{
for (int c = menu.Count - 1; c >= 0; c--)
{
var key = menu.Keys.ElementAt(c);
var value = menu[key];
if (value is Dictionary<string, object>)
{
RemoveNotPermittedItems((Dictionary<string, object>)value);
if (((Dictionary<string, object>)value).Count == 0)
{
menu.Remove(key);
}
}
else if (!GetIsPermitted(value))
{
menu.Remove(key);
}
}
}
private static bool GetIsPermitted(object value)
{
if (counter++ % 2 == 0)
return false;
return true;
}
"if", , ... , "GetIsPermitted" TRUE ActionDictionary.
, .