- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在尝试迭代包含对象作为值的字典:
foreach (KeyValuePair<int, CMapPool_Entry> Entry in MapPool)
{
this.SendConsoleMessage(Entry.Value.Map);
}
下面可以看到CMapPool_Entry类
public class CMapPool_Entry
{
public string Map;
public string Mode;
public int Rounds;
public int Index;
public int Votes;
public bool Nominated;
public string Nominator;
public CMapPool_Entry(string map, string mode, int rounds, int index, string Nominator_LeaveEmptyStringIfNone)
{
this.Map = map;
this.Mode = mode;
this.Rounds = rounds;
this.Index = index;
// If Nominator isn't empty, set map to nominated
if (Nominator_LeaveEmptyStringIfNone != "")
{
this.Nominated = true;
this.Nominator = Nominator_LeaveEmptyStringIfNone;
}
}
public void AddVote()
{
this.Votes++;
}
public void RemoveVote()
{
if (this.Votes > 0)
this.Votes--;
}
}
这里还可以看到SendConsoleMessage方法:
private void SendConsoleMessage(string message)
{
this.ExecuteCommand("procon.protected.pluginconsole.write", String.Format("{0}", message));
}
对我来说它看起来可行,我已经读到如何在 foreach 中编辑字典中的值会产生以下错误:“集合已修改;枚举操作可能无法执行。”
但为什么会出现此错误?我没有编辑任何值,我只是在阅读它们,对吗?如果 CObject 是 string 或 int,它工作正常,但如果它是一个对象,它就会发疯。我做错了什么,我该怎么办?
编辑:进一步调试后,我注意到 Entry.Key 可以正常使用,但只要我触摸 Entry.Value,我就会收到错误消息。由于某种原因,我随机得到了两个不同的错误:
有什么想法吗?还是以对象作为值枚举字典根本行不通?
最佳答案
正如每个人在评论中指出的那样,您在尝试阅读字典时正在另一个线程中修改字典。您在评论中写道,您通过以下方式“复制”了词典:
Dictionary<int, CMapPool_Entry> MapPool = this.Votemap_MapPool;
这不会复制字典,而是在 MapPool
中创建对 Votemap_MapPool
的引用,因此在修改代码中某处的 Votemap_MapPool
时,同时读取 MapPool
您将收到异常 System.InvalidOperationException
,并显示集合已修改的消息。
要真正复制你的字典,你必须写:
Dictionary<int, CMapPool_Entry> MapPool = new Dictionary<int, CMapPool_Entry>(Votemap_MapPool);
这实际上创建了一个以 Votemap_MapPool
作为内容的 Dictionary 的新实例,当 Votemap_MapPool
被修改时,MapPool
保持不变。
关于c# - 在包含对象作为值的字典上使用 foreach 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11483232/
这是一个假设性问题。如果我有来自 3 个单独的 sql db 查询的 3 个数组,这些查询都与另一个数组相关。例如…… //db schools id | school_name classes id
在我的应用程序中,我使用 scrape(string url) 方法从网页中抓取链接。可以说它每次都返回我 10 个 url。 我想从每个抓取的 url 中抓取 10 个链接。 长话短说: (第 1
我的java7代码: final Map result = new HashMap<>(); final Set> classes = getClasses(co.glue()); for (fina
是否可以在 SwiftUI 中设置变量,例如在这样的 ForEach 中: struct ContentView: View { var test: Int var body: som
在 D、int、uint 中使用 foreach 时,循环索引的首选类型是什么?或者只是通过省略类型自动实现? 最佳答案 一般来说,索引应该是size_t。与长度相同。如果您尝试使用 int 或 ui
根据 http://dlang.org/statement.html 的“Foreach 限制”部分以下代码 int[] a; int[] b; foreach (int i; a) { a
在什么情况下我们应该在 JDK 8 中使用旧的 foreach 循环遍历新的 collection.forEach() 还是最好的做法是转换 every foreach 循环?是否存在任何重要的性能差
获得类似东西的惯用方法是什么? ((fn [coll] (function-body)) [:a :b :c :d]) -> [[:a :b][:a :c][:a :d][:b :c][:b :d][
我正在创建一个基于 who is it? 的 Java 应用程序。现在我正在制作一种方法,在回答问题时我需要其他卡片。 我有两个列表: 列表是一个 ImageView 列表,其中我有卡片必须代表的 2
我希望有人能在我发疯之前帮助我。 我有 3 张 table : Table A SELECT companypk, companyname, logo, msscope FROM global_com
我正在尝试将多个字符串添加到 C# 中的 MailAddress。 如果我使用ForEach,我的代码会是这样 foreach (var item in GetPeopleList()
我没有太多的 C# 经验,所以如果有人能指出正确的方向,我将不胜感激。我有一个引用对象变量的 foreach 循环。我希望在主循环中创建另一个 foreach 循环,将当前变量与对象数组中的其余变量进
下面的代码每 60 秒删除文件夹“Images”中的文件,它可以工作,但是当文件夹为空时它会显示:警告:为 foreach() 提供的参数无效如果没有文件,如何解决这个问题,说“文件夹为空而不是那个警
我需要在两种不同的模式下运行,因此“if”(第二个稍后构建一个大的 csv) 下面对于单个实例运行正常,但在第二个 (*) 的加载时间上失败,因为在前 7k 行中的每一行上运行。 我想避免可怕的事情
我们可以使用以下两种方法实现类数组对象的迭代: let arrayLike = document.getElementsByClassName('dummy'); [].forEach.call(ar
我有这个代码 ... 它说: Attribute value invalid for tag forEach according to TLD 最佳答案 forEach标签不支持 valu
我在 SwiftUI 中有一个像这样的 ForEach: ForEach(entries) { (e: MyType) in NavigationLinkItem(entry: e) } 现在我
我无法在一个 Foreach 或 Foreach-Object 循环中使用多个命令 我的情况是—— 我有很多文本文件,大约 100 个。 所以他们被阅读 Get-ChildItem $FilePath
我必须从 json 文件(实际上是 2 个 json 文件)执行 ForEach,因此我执行 2 forEach,代码是 table { font-family: arial, sans-
我对编程很陌生,当我执行 forEach 函数时,我的应用程序返回错误。我的controller.js中有以下代码 $scope.ajaxRequest = A.Game.get({action: '
我是一名优秀的程序员,十分优秀!