[英]How to override list configuration in .net
I have hard time to find out solution for an issue regarding binding values from configuration files.我很难找到有关配置文件绑定值问题的解决方案。
I have configuration option containing list initialized in code, for example例如,我有包含在代码中初始化的列表的配置选项
public List<string> availableFruit { get; set; } = new List<string> { "apple", "banana", "pineapple",};
But I want to change it in config file where I have但我想在配置文件中更改它
{
"Store": {
"AvailableFruit": [
"watermelon"
]
}
}
The binding now works that way, that it adds config in file to the previously initialized in code, thus it results in "apple, banana, pineapple, watermelon", but I want to have only watermelon.绑定现在以这种方式工作,它将文件中的配置添加到先前在代码中初始化的配置,因此结果为“苹果、香蕉、菠萝、西瓜”,但我只想吃西瓜。
I digged through code and found out, that .net has it hardcoded in configuration binder that it uses "Add" method.我仔细研究了代码,发现 .net 在配置活页夹中进行了硬编码,它使用“添加”方法。 I also tried to create new method where I wanted override the method, but that was not possible.
我还尝试在我想覆盖该方法的地方创建新方法,但这是不可能的。
Is there any way, how can I do that?有什么办法,我该怎么做?
//EDIT: I call it as this //编辑:我这样称呼它
services.Configure<StoreOptions>(Configuration.GetSection("Store"));
public class JsonHelper
{
/**
* JsonHelper.JsonFileAddParameter("~/app.json", "hot", "YEEE");
* */
public static void JsonFileAddParameter(string path, string parameter, string value)
{
if (String.IsNullOrEmpty(value))
throw new ArgumentNullException("Value");
if (String.IsNullOrEmpty(parameter))
throw new ArgumentNullException("Parameter");
dynamic treatments;
string _path = HostingEnvironment.MapPath(path);
using (StreamReader sr = new StreamReader(_path))
{
treatments = JsonConvert.DeserializeObject(sr.ReadToEnd());
sr.Close();
}
//JObject property = new JObject( new JProperty(parameter, value) );
treatments[parameter] = value;
File.WriteAllText(_path, String.Empty);
string json = JsonConvert.SerializeObject(treatments);
File.WriteAllText(_path, json);
}
/**
* JsonHelper.JsonFileEdit("~/app.json", "AppKey", "123456789");
* */
public static void JsonFileEdit(string path, string parameter, string editValue)
{
if (String.IsNullOrEmpty(editValue))
throw new ArgumentNullException("Edit Value");
if (String.IsNullOrEmpty(parameter))
throw new ArgumentNullException("Parameter");
dynamic treatments;
string _path = HostingEnvironment.MapPath(path);
using (StreamReader sr = new StreamReader(_path))
{
treatments = JsonConvert.DeserializeObject(sr.ReadToEnd());
sr.Close();
}
if (treatments[parameter] == null)
throw new ArgumentNullException(parameter);
treatments[parameter] = editValue;
File.WriteAllText(_path, String.Empty);
string json = JsonConvert.SerializeObject(treatments);
File.WriteAllText(_path, json);
}
/**
* var lol = JsonHelper.JsonReader("~/app.json", "AppKey");
* */
public static dynamic JsonReader(string path, string parameter)
{
dynamic treatments;
using (StreamReader sr = new StreamReader(HostingEnvironment.MapPath(path)))
{
treatments = JsonConvert.DeserializeObject(sr.ReadToEnd());
sr.Close();
}
return treatments[parameter];
}
/**
* var lol = JsonHelper.Json("~/package.json");
* */
public static dynamic JsonReader(string path)
{
/**
* using(StreamReader sr = new StreamReader(Server.MapPath("~/Content/treatments.json")))
{
treatments = JsonConvert.DeserializeObject<List<Treatment>>(sr.ReadToEnd());
}
* */
dynamic treatments;
using (StreamReader sr = new StreamReader(HostingEnvironment.MapPath(path)))
{
treatments = JsonConvert.DeserializeObject(sr.ReadToEnd());
sr.Close();
}
return treatments;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.