c# - Set value of specific property by custom attribute -


i developing software used users should not able access back-end of should still able change configuration/settings application. decided best approach custom "configuration file (.cfg)" located in root of final build. simple example of .cfg file:

serveraddress='10.10.10.10' serverport='1234' servertimeout='15000' 

since wanted configuration file extended decided use custom attributes , simple linq. work expect to, since still novice in .net afraid have not gone best approach , question therefor: there can improve this? or there better approach this?

this code reading configuration file , assigning values it's corresponding properties.

configfilehandler.cs

public void readconfigfile()     {         var cfgfile = new configfile();         var configlines = file.readalllines("configfile.cfg");         var testlist = configlines.select(line => line.split('='))             .select(splitstring => new tuple<string, string>(splitstring[0], splitstring[1].replace("'", "")))             .tolist();         foreach (var prop in typeof(configfile).getproperties())         {             var attrs = (configfilefieldattribute[])prop.getcustomattributes                 (typeof(configfilefieldattribute), false);             foreach (var t in attr in attrs t in testlist t.item1 == attr.name select t)             {                 prop.setvalue(cfgfile, t.item2);             }         }     } 

configfile.cs

 class configfile     {         private static string _serveraddress;         private static int _serverport;         private static int _servertimeout;          [configfilefield(@"serveraddress")]         public string serveraddress         {             { return _serveraddress; }             set { _serveraddress= value; }         }          [configfilefield(@"serverport")]         public string serverport         {             { return _serverport.tostring(); }             set { _serverport= int.parse(value); }         }          [configfilefield(@"servertimeout")]         public string servertimeout         {             { return _servertimeout.tostring(); }             set { _servertimeout= int.parse(value); }         }     } 

any tips on writing better looking code highly appreciated!


update: feedback.

below final classes! https://dotnetfiddle.net/bpmnja live example

please note, c# 6.0

configfilehandler.cs

 public class configfilehandler  {     public void readconfigfile()     {         var configlines = file.readalllines("configfile.cfg");         var configdictionary = configlines.select(line => line.split('='))         .select(splitstring => new tuple<string, string>(splitstring[0],     splitstring[1].replace("'", "")))         .todictionary(kvp => kvp.item1, kvp => kvp.item2);         configfile.setdictionary(configdictionary);     }  } 

configfile.cs

 public class configfile  {     private static dictionary<string, string> _configdictionary;      public string serveraddress => pullvaluefromconfig<string>("serveraddress", "10.1.1.10");      public int serverport => pullvaluefromconfig<int>("serverport", "3306");      public long servertimeout => pullvaluefromconfig<long>("servertimeout", "");       private static t pullvaluefromconfig<t>(string key, string defaultvalue)     {         string value;         if (_configdictionary.trygetvalue(key, out value) && value.length > 0)             return (t) convert.changetype(value, typeof (t));         return (t) convert.changetype(defaultvalue, typeof (t));      }      public static void setdictionary(dictionary<string, string> configvalues)     {         _configdictionary = configvalues;     }  } 

you keep simplicity of config file , rid of nested loops loading values dictionary , passing configfile class.

    public static void readconfigfile()     {         var configlines = file.readalllines("configfile.cfg");         var testlist = configlines.select(line => line.split('='))             .select(splitstring => new tuple<string, string>(splitstring[0], splitstring[1].replace("'", "")))             .todictionary(kvp => kvp.item1, kvp => kvp.item2);          var cfgfile = new configfile(testlist);     } 

the new configfile class:

class configfile {     private dictionary<string, string> _configdictionary;      public configfile(dictionary<string, string> configvalues)     {         _configdictionary = configvalues;     }      public string serveraddress     {         { return pullvaluefromconfig("serveraddress", "192.168.1.1"); }     }      public string serverport     {         { return pullvaluefromconfig("serverport", "80"); }     }      public string servertimeout     {         { return pullvaluefromconfig("servertimeout", "900"); }     }      private string pullvaluefromconfig(string key, string defaultvalue)     {             string value;             if (_configdictionary.trygetvalue(key, out value))                 return value;             return defaultvalue;     } } 

Comments

Popular posts from this blog

shopping cart - Page redirect not working PHP -

php - How to modify a menu to show sub-menus -

python - Installing PyDev in eclipse is failed -