我试图使用dict中的值副本为每个循环生成线程.
我最初的理解是,这foreach将创造一个新的范围,并导致:
Dictionary<string, string> Dict = new Dictionary<string, string>() { { "sr1", "1" }, { "sr2", "2" } };
foreach (KeyValuePair<string, string> record in Dict) {
new System.Threading.Timer(_ =>
{
Console.WriteLine(record.Value);
}, null, TimeSpan.Zero, new TimeSpan(0, 0, 5));
}
Run Code Online (Sandbox Code Playgroud)
写道
1
2
2
2
Run Code Online (Sandbox Code Playgroud)
而不是(预期):
1
2
1
2
Run Code Online (Sandbox Code Playgroud)
所以我尝试在foreach中克隆kvp:
KeyValuePair<string, string> tmp = new KeyValuePair<string, string>(record.Key, record.Value);
Run Code Online (Sandbox Code Playgroud)
但这会产生相同的结果.
我也试过了,System.Parallel.ForEach但似乎需要非动态的价值,这对我的字典来说有点火车粉碎.
如何用线程迭代我的字典?
跟进:
如果我使用本地范围的变量运行一个函数,我的程序最终会抛弃该值 - 这让我完全糊涂了.
为什么会这样?
简单示例:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
Dictionary<string, object> Dict = new Dictionary<string, object>() {
{ "1", new Dictionary<string, object>() {
{"Columns",new object[1]{ "name"} }
}},
{ "2", new Dictionary<string, object>() {
{"Columns",new object[1]{ "name"} }
}}
};
int i = 0;
foreach (KeyValuePair<string, object> record in Dict)
{
var _recordStored = record; //THIS IS(was) SUPPOSED TO …Run Code Online (Sandbox Code Playgroud) 我想弄清楚ExtJS4如何传递配置对象.我想做相当于......
store = function(config){
if ( typeof config.call !== 'unndefined' ){
config.url = "server.php?c=" + config.call || config.url;
};
Sketch.Data.AutoSaveStore.superclass.constructor.call(this,config);
};
Ext.extend(store, Ext.data.Store{})
Run Code Online (Sandbox Code Playgroud)
我可能在这里遗漏了一些明显的东西,但是在沙盒文件中挖了一下,我最接近的就是....
Ext.define('My.awesome.Class', {
// what i would like to pass.
config:{},
constructor: function(config) {
this.initConfig(config);
return this;
}
});
Run Code Online (Sandbox Code Playgroud)
如果你做的事情似乎不起作用......
var awesome = Ext.create('My.awesome.Class',{
name="Super awesome"
});
alert(awesome.getName()); // 'awesome.getName is not a function'
Run Code Online (Sandbox Code Playgroud)
然而
Ext.define('My.awesome.Class', {
// The default config
config: {
name: 'Awesome',
isAwesome: true
},
constructor: function(config) {
this.initConfig(config);
return this;
}
});
var …Run Code Online (Sandbox Code Playgroud) 我最近看到了一个有趣的代码库,其中一些参数通过注释传递给方法.它看起来像
/*
* @attribute default-method
*/
Run Code Online (Sandbox Code Playgroud)
当然,这将声明实例化的类的默认方法.
这是什么?怎么做?在任何地方都有这样的例子吗?
它被用来控制返回的格式等,所以它看起来非常有趣,肯定会从我创建的类中删除很多垃圾.
如果一个值必须定义为true或false,基于检查另一个值,是否有任何问题:
!!someValue
Run Code Online (Sandbox Code Playgroud)
只需快速对抗chrome的引擎即可:
boolCheck=function(someValue){
return !! someValue;
}
console.log(boolCheck()); //false
console.log(boolCheck(undefined)); //false
console.log(boolCheck(null)); //false
console.log(boolCheck(1)); //true
console.log(boolCheck({va:1})); //true
console.log(boolCheck("someTS")); //true
console.log(boolCheck(boolCheck)); //true
Run Code Online (Sandbox Code Playgroud)
SEEMS工作....