.net - C# AppSettings array -
i have string constants need access multiple files. since values of these constants might change time time, decided put them in appsettings rather constants class don't have recompile every time change constant.
sometimes need work individual strings , need work of them @ once. i'd this:
<?xml version="1.0" encoding="utf-8"?> <configuration> <appsettings> <add key="const1" value="hi, i'm first constant." /> <add key="const2" value="i'm second." /> <add key="const3" value="and i'm third." /> <add key="const_arr" value=[const1, const2, const3] /> </appsettings> </configuration>
the reasoning being i'll able stuff like
public dictionary<string, list<double>> getdata(){ var ret = new dictionary<string, list<double>>(); foreach(string key in configurationmanager.appsettings["const_arr"]) ret.add(key, foo(key)); return ret; } //... dictionary<string, list<double>> dataset = getdata(); public void processdata1(){ list<double> data = dataset[configurationmanager.appsettings["const1"]]; //... }
is there way this? i'm pretty new , concede might horrendous design.
you not need put array of key in appsettings
key can iterate keys of appsetting code itself. so, appsettings
should :
<appsettings> <add key="const1" value="hi, i'm first constant." /> <add key="const2" value="i'm second." /> <add key="const3" value="and i'm third." /> </appsettings>
after this, can create global static dictionary can access part of program :
public static dictionary<string, list<double>> dataset { { var ret = new dictionary<string, list<double>>(); // iterate through each key of appsettings foreach (string key in configurationmanager.appsettings.allkeys) ret.add(key, foo(configurationmanager.appsettings[key])); eturn ret; } }
as foo method
has been accessed static
property, need define foo method static method. so, foo method should :
private static list<double> foo(string key) { // process , return value return enumerable.empty<double>().tolist(); // returning empty collection demo }
now, can access dataset dictionary
key in following way :
public void processdata1() { list<double> data = dataset["const1"]; //... }
Comments
Post a Comment