Generate properties programmatically

前端 未结 7 843
难免孤独
难免孤独 2021-01-04 17:47

I want to load a properties file (it\'s a .csv file having on each line a name and associated numeric value) and then access those property values like so: FileLoader

7条回答
  •  星月不相逢
    2021-01-04 18:48

    Here's a sample using ExpandoObject and C#`s 4.0 dynamic feature

    public dynamic ParseCsvFile(string filePath) {
      var expando = new ExpandoObject;
      IDictionary map = expando;
      foreach ( var line in File.ReadAllLines(filePath)) {
        var array = line.Split(new char[]{','},2);
        map.Add(array[0],array[1]);
      }
      return expando;
    }
    
    ...
    var d = ParseCsvFile(someFilePath);
    Console.WriteLine(d.Property1);
    

提交回复
热议问题