c# - How to refactor highly repetitive reading sections from config files -



c# - How to refactor highly repetitive reading sections from config files -

i need convert multiple sections of config file dictionaries. values of dictionaries have different types. next 2 classes work, identical:

public class intconfigsection { private static readonly ilog log = logmanager.getlogger(typeof(intconfigsection)); public static dictionary<string, int> loadsection(string sectionname) { var ret = new dictionary<string, int>(); seek { var offsetshash = (hashtable)configurationmanager.getsection(sectionname); foreach (dictionaryentry entry in offsetshash) { ret.add((string)entry.key, int.parse((string)entry.value)); } } catch(exception e) { log.errorformat("loadsection:" + e); } homecoming ret; } } public class stringconfigsection { private static readonly ilog log = logmanager.getlogger(typeof(stringconfigsection)); public static dictionary<string, string> loadsection(string sectionname) { var ret = new dictionary<string, string>(); seek { var offsetshash = (hashtable)configurationmanager.getsection(sectionname); foreach (dictionaryentry entry in offsetshash) { ret.add((string)entry.key, (string)entry.value); } } grab (exception e) { log.errorformat("loadsection:" + e); } homecoming ret; } }

the next code not work required, demonstrates trying accomplish:

public class configsection<t> { private static readonly ilog log = logmanager.getlogger(typeof(stringconfigsection)); public static dictionary<string, t> loadsection(string sectionname) { var ret = new dictionary<string, t>(); seek { var offsetshash = (hashtable)configurationmanager.getsection(sectionname); foreach (dictionaryentry entry in offsetshash) { //builds not want ret.add((string)entry.key, (t)entry.value); // not compile //ret.add((string)entry.key, t.parse((string)entry.value)); } } grab (exception e) { log.errorformat("loadsection:" + e); } homecoming ret; } }

edit: final version looks follows:

public class configsectionloader { public static dictionary<string, int> loadintsection(string sectionname) { homecoming configsection<int>.loadsection(sectionname, int.parse); } public static dictionary<string, string> loadstringsection(string sectionname) { homecoming configsection<string>.loadsection(sectionname, val => val); } } internal class configsection<t> { private static readonly ilog log = logmanager.getlogger(typeof(stringconfigsection)); internal static dictionary<string, t> loadsection(string sectionname, func<string, t> parsefunc) { var ret = new dictionary<string, t>(); seek { var hash = (hashtable)configurationmanager.getsection(sectionname); foreach (dictionaryentry entry in hash) { ret.add((string)entry.key, parsefunc((string)entry.value)); } } grab (exception e) { log.errorformat("loadsection:" + e); } homecoming ret; } }

my concern this: val => val simplest lambda nothing?

you pass in function parsing type: (note: untested code, consider thought rather working code :d)

public class configsection<t> { private static readonly ilog log = logmanager.getlogger(typeof(stringconfigsection)); public static dictionary<string, t> loadsection(string sectionname, func<string,t> parsefunc) { var ret = new dictionary<string, t>(); seek { var offsetshash = (hashtable)configurationmanager.getsection(sectionname); foreach (dictionaryentry entry in offsetshash) { ret.add((string)entry.key, parsefunc((string)entry.value)); } } grab (exception e) { log.errorformat("loadsection:" + e); } homecoming ret; } }

and pass in actual parser lambda (or similar..)

or include parser in constructor class , have fellow member don't have pass in every time.

public class configsection<t> { private func<string, t> myparsefunc = null; public configsection<t>(func<string,t> parparsefunc) { myparsefunc = parparsefunc; } private static readonly ilog log = logmanager.getlogger(typeof(stringconfigsection)); public static dictionary<string, t> loadsection(string sectionname) { var ret = new dictionary<string, t>(); seek { var offsetshash = (hashtable)configurationmanager.getsection(sectionname); foreach (dictionaryentry entry in offsetshash) { ret.add((string)entry.key, myparsefunc((string)entry.value)); } } grab (exception e) { log.errorformat("loadsection:" + e); } homecoming ret; } }

and phone call so:

configsection<int> = new configsection<int>(int.parse);

c# templates configuration refactoring

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -