在C#Hashtable上迭代并更改值.其他信息:收集修改;枚举操作可能不会执行



我正在尝试通过标志性迭代并更新该值,只要它通过某些条件即可。这是代码。

public static Hashtable AssembliesGlobal= new Hashtable();
public static void populateHash(){
   NDepend.Path.IAbsolutePath assemblyName; 
   NDepend.Path.IAbsolutePath projectName;
   for (int i = 0; i < someList.Count(); i++)
      {
         if (someList[i].AssembliesUsed.Count() > 0)
            {
                assemblyName = getAssemblyQuery[i].a.FilePath;
                if (getAssemblyQuery[i].a.VisualStudioProjectFilePath != null)
                {
                    List<IAbsolutePath> thirdPartyList = new List<IAbsolutePath>();
                    projectName = getAssemblyQuery[i].a.VisualStudioProjectFilePath;
                    thirdPartyList.Add(assemblyName);
                    AssembliesGlobal.Add(projectName, thirdPartyList);
                }
            }
        }
     }

public static void parseCsproj()
{
    foreach (IAbsoluteFilePath key in AssembliesGlobal.Keys)
    {
        XmlDocument xmldoc = new XmlDocument();
        xmldoc.Load(key.FileInfo.FullName);
        XmlNodeList references = xmldoc.GetElementsByTagName("ProjectReference");
        XmlNodeList hintReferences = xmldoc.GetElementsByTagName("HintPath");
        if (references.Count >= 1 || hintReferences.Count >= 1)
        {
            for (int i = 0; i < references.Count; i++)
            {
                string path = references[i].Attributes[0].Value;
                if(path.Contains("3rdParty")){
                    AssembliesGlobal[key] = path;
                }
            }
            for (int i = 0; i < hintReferences.Count; i++)
            {
                string path = hintReferences[i].InnerText;
                if (path.Contains("3rdParty"))
                {
                    AssembliesGlobal[key] = path;
                }
            }
        }
    }
}

assembliesglobal是一个具有:

的结构的标签。

AssembliesGlobal = {key => []}

我想附加到键值的数组。在与VS中运行调试器后,我不断获得其他信息:收集进行了修改;枚举操作可能不会执行。错误。我知道您不能迭代地更新一个标签,我想知道在这种情况下可能有什么作品。我是这个语言的新手。

您可以通过施放和添加 .ToList()

将列表的副本进行迭代,以迭代遍历。
foreach (var key in AssembliesGlobal.Keys.Cast<IAbsoluteFilePath>().ToList())

请注意,您需要.Cast,因为Hashtable是非生成的。正如其他人提到的那样,现代的方法是Dictionary<K,V>

回应您对上面评论的评论,是您所追求的(请记住,这是一个被剥夺的示例)。

var AssembliesGlobal = new Dictionary<string, List<string>>();
AssembliesGlobal.Add("Steve", new List<string> { "1", "2", "3"});
foreach (var key in AssembliesGlobal.Keys)
{
    AssembliesGlobal[key].Add("4");
}

最新更新