我在下面定义了变量:
Dictionary<clssMenu_Item, Dictionary<clssMenu_Item, List<clssMenu_Item>>> dicMenu =
new Dictionary<clssMenu_Item, Dictionary<clssMenu_Item, List<clssMenu_Item>>>();
类型clssMenu_Item
定义为:
class clssMenu_Item
{
public string name;
public string text;
}
我有一个字符串(我称之为strKey
)已经分配到text
clssMenu_Item
内的dicMenu
属性。很明显text
是key
的一部分。我想在dicMenu
中搜索value
strKey
的{{1}}字典,例如其子字典。我不知道怎么能得到这个值?!
此方法计算字典的key
:
private List<clssMenu_Item> GetFormButtons(Form form)
{
List<clssMenu_Item> ListBtn = new List<clssMenu_Item>();
foreach (Control c in GetAllControls<Button>(form))
{
int btnIndex= c.Name.IndexOf("btn");
if (btnIndex != 0) // find all of the determined buttons
{
clssMenu_Item objBtn = new clssMenu_Item();
objBtn.name = c.Name.ToString();
objBtn.text = c.Text;
ListBtn.Add(objBtn);
}
}
return ListBtn;
}
这是我填充密钥的方式:
foreach (var k in objH.mainForm) //fill dicMenu
{
dicMenu.Add(k,null);
}
我使用嵌套字典的原因是这棵树只有3个级别。每个clssMenu_Item
都有一组像他们一样的孩子type
。 dictionary
是我拯救他们的解决方案。
问题: 现在我想通过以下代码填写键的值:
Dictionary<clssMenu_Item, List<clssMenu_Item>> d2 = new Dictionary<clssMenu_Item, List<clssMenu_Item>>();
clssMenu_Item k = new clssMenu_Item();
k.text = strKey;
foreach(var prp in dicMenu.Keys) //we should fill all of the variables in class
{
if (prp.text == strKey)
{ k.name = prp.name; break; }
}
d2= dicMenu[k]; //assign value of key
我不知道为什么我在d2= dicMenu[k];
上有错误说k
不在DicMenu
,尽管我调试了这个块。我们在k
中DicMenu
:(我不知道该怎么办?!
还有其他方法可以创建树吗?