我在.Net 4.5上使用Json.Net,当在下面的对象上使用populate对象时,它会使用json的内容增加List,而不是设置它的值.
Json.Net
JsonConvert.PopulateObject(string, object)
Run Code Online (Sandbox Code Playgroud)
类
class MySettingSubClass
{
public List<string> MyStringList1 = new List<string>(){"one", "two", "three"}
}
class MySetting
{
public string MyString = "MyString";
public int MyInt = 5;
public MySettingSubClass MyClassObject = new MySettingSubClass();
public List<string> MyStringList2 = new List<string>{"one", "two", "three"};
}
Run Code Online (Sandbox Code Playgroud)
当他们最初加载时,一切都是正确的,但是从JSON重新加载两个MyStringLists都是重复的 "one", "two", "three", "one", "two", "three"
我希望将一个大型配置.js文件拆分成多个较小的文件,但仍然将它们组合到同一个模块中.这是常见做法,最佳方法是什么,以便在添加新文件时模块不需要扩展.
添加新文件时的示例,例如但不需要更新math.js.
math - add.js - subtract.js - math.js
// add.js
module.exports = function(v1, v2) {
return v1 + v2;
}
// subtract.js
module.exports = function(v1, v2) {
return v1 - v2;
}
// math.js
var add = require('./add');
exports.add = add;
var subtract = require('./subtract');
exports.subtract = subtract;
// app.js
var math = require('./math');
console.log('add = ' + math.add(5,5));
console.log('subtract =' + math.subtract(5,5));
Run Code Online (Sandbox Code Playgroud) 我遇到的情况是,我试图访问一个静态属性,该属性包含一个对象的单例,我希望仅通过知道其类型来检索该对象。我有一个实现,但看起来很麻烦......
public interface IFace
{
void Start()
}
public class Container
{
public IFace SelectedValue;
public Type SelectedType;
public void Start()
{
SelectedValue = (IFace)SelectedType.
GetProperty("Instance", BindingFlags.Static | BindingFlags.Public).
GetGetMethod().Invoke(null,null);
SelectedValue.Start();
}
}
Run Code Online (Sandbox Code Playgroud)
有没有其他方法可以做到以上几点?使用 System.Type 访问公共静态属性?
谢谢