有人有更巧妙的方法来做到这一点吗?似乎它应该比这更容易,但我有一个心理障碍。基本上我需要从字典中删除项目并递归到也是字典的项目的值。
private void RemoveNotPermittedItems(ActionDictionary menu)
{
var keysToRemove = new List<string>();
foreach (var item in menu)
{
if (!GetIsPermitted(item.Value.Call))
{
keysToRemove.Add(item.Key);
}
else if (item.Value is ActionDictionary)
{
RemoveNotPermittedItems((ActionDictionary)item.Value);
if (((ActionDictionary)item.Value).Count == 0)
{
keysToRemove.Add(item.Key);
}
}
}
foreach (var key in (from item in menu where keysToRemove.Contains(item.Key) select item.Key).ToArray())
{
menu.Remove(key);
}
}
动作字典是这样的:
public class ActionDictionary : Dictionary<string, IActionItem>, IActionItem