从这个答案我学会了如何在c#中展平JSON对象.来自JSON字符串:
{"menu": {
"id": "file",
"value": "File",
"popup": {
"menuitem": [
{"value": "New", "onclick": "CreateNewDoc()"},
{"value": "Open", "onclick": "OpenDoc()"},
{"value": "Close", "onclick": "CloseDoc()"}
]
}
}}
Run Code Online (Sandbox Code Playgroud)
至:
以下是字符串行,而不是对象
menu.id:file
menu.value:File
menu.popup.menuitem[0].value:New
menu.popup.menuitem[0].onclick:CreateNewDoc()
menu.popup.menuitem[1].value:Open
menu.popup.menuitem[1].onclick:OpenDoc()
menu.popup.menuitem[2].value:Close
menu.popup.menuitem[2].onclick:CloseDoc()
Run Code Online (Sandbox Code Playgroud)
现在,我想扭转这个过程.我可以从这个问题找到实现,但它是在JavaScript中.
如何unflatten(返回从行结构化JSON)在C#与json.net?
我在这种情况下,有一个大字典,由一个线程以相当高的频率随机更新,还有另一个线程试图将字典的快照保存为历史记录.我目前正在使用这样的东西:
Dictionary<string, object> dict = new Dictionary<string, object>();
var items = dict.Values.ToList();
Run Code Online (Sandbox Code Playgroud)
这在大多数情况下都可以正常工作,除非它偶尔抛出:
System.InvalidOperationException:集合已被修改; 枚举操作可能无法执行.
我理解为什么会发生这种情况,但我不知道我该怎么做才能避免收集修改错误.
迭代此类集合的最佳方法是什么?
我也试过ConcurrentDictionary,但没有运气.为什么?ConcurrentDictionary线程只在项目级别安全吗?
So I read through the documentation of Microsoft here.
Consider the following code:
int i = 0;
object o = i;
object p = o;
o = 1;
p = 2;
Console.WriteLine($"o:{o}, p:{p}");
//output o:1, p:2
Run Code Online (Sandbox Code Playgroud)
My understanding is that boxing happen on object o = i;, now o is a refence to the value in heap. Then p is assigned to be same as o.
Why isn't the change of p refected to o? Aren't …
c# ×3
boxing ×1
concurrency ×1
dictionary ×1
flatten ×1
ienumerable ×1
json ×1
reference ×1
types ×1
unboxing ×1