c# - reading files using generic array ref -


i working on small program has save , open functionality bunch of variables. variables of various types, int - string - double , arrays

i implement function can cope different data types

let's have following variables

int var_int_1 = 1; double var_double_1 = 2.2; string  var_string_1 = "three"; int[] arr_int_1 = new int[5]; 

the saved file looks this

var_int_1: 1 var_double_1: 2.2 var_string_1: "three" arr_int_1:    arr_int_1_00: 1;    arr_int_1_01: 2; 

my open-function this

public bool open(string filename) {     try     {         string line;         using (streamreader file = new streamreader(filename))         {             while ((line = file.readline()) != null)             {                 extract_value_from_stream("var_int_1", line, ref var_int_1);                 extract_value_from_stream("var_double_1", line, ref var_double_1);                 extract_value_from_stream("var_string_1", line, ref var_string_1);                 extract_value_from_stream("arr_int_1", line, ref arr_int_1);             }         }     }     catch ...        

the extract_value_from_stream function:

public void extract_array_from_stream<t>(string str_name, string line, ref t var_name)     {             if (typeof(t).isarray) //t array => return array element             {                 string index;                 str_name += "_";     //add "_" name                  index = line.substring(line.indexof(str_name) + str_name.length,2);     //read index - 2 positions after str_name                  var_name[int.parse(index)] = (t)convert.changetype(line, typeof(t));             }             else  //if no array => return single value             {                 str_name += ": ";                  //add ":" name (see file)                 if (line.contains(str_name))       //if line contains name                 {                     line = line.substring(line.indexof(str_name) + str_name.length);    //data = substring after str_name                     var_name = (t)convert.changetype(line, typeof(t));                  //cast line correct type var_name                 }                       }         } 

the no-array part works nicely. on array part generates error:

cannot apply indexing [] expression of type t

is there way solve this?

the propper solution error message be.. add constraint generic method call. you'll end using system.collections namespace. not usual system.collections.generic, you're able keep it.. try implementing this..

// note can add base layer logic generic instance ... public void extractarrayfromstream<t>(string x, string y, ref t array) t : ilist {     // logical magic done here.. } 

for more information using contraints visit documentation.


edit...

this skip part check whether it's array or not.


Comments

Popular posts from this blog

java - Date formats difference between yyyy-MM-dd'T'HH:mm:ss and yyyy-MM-dd'T'HH:mm:ssXXX -

c# - Get rid of xmlns attribute when adding node to existing xml -