0

I have a C# project in SharpDevelop. I define the data variables in a class called, thusly:

using System;
namespace CSVHelperReadSample
{
    class DataRecord1
    {
        public string TimeWhen { get; set;}
        public float IconAltitude { get; set; }
        public float Latitude { get; set; }
    }
}

Can I dynamically change the declarations in DataRecord1 by reading in a csv file? The csv file would have four columns and n rows, where column 1 = public, column 2 = type (string, float), column 3 = dataFieldName, and column 4 = { get; set;}.

Benjamin Levy
  • 333
  • 6
  • 19
  • 1
    So you want to set your properties based on what you find in the csv file? – Broots Waymb Oct 27 '16 at 18:28
  • Once a class is compiled, it is that until you change the source and recompile. If you are looking to dynamically add properties, see [here](http://stackoverflow.com/questions/947241/how-do-i-create-dynamic-properties-in-c) If you are trying to read from a variable length/width item like a csv file, you might consider a dictionary rather than a class. – Marc Lyon Oct 27 '16 at 18:33
  • I just want to be able to set the class definitions externally at the time of compilation, based on reading a csv file that contains the declarations. – Benjamin Levy Oct 27 '16 at 18:35
  • The C# compiler will only compile C# code. You'll need to either write your own compiler to "compile" CSV data (one does not exist that I know of) or use something like [T4 templates](https://msdn.microsoft.com/en-us/library/bb126445.aspx) to generate C# code from your CSV file. – D Stanley Oct 27 '16 at 19:03
  • I've experimented with T4 templates per @D-Stanley (e.g., https://msdn.microsoft.com/en-us/library/dd820620.aspx). I'm trying to modify this example to read the variable properties from a csv file with using (var sr = new StreamReader(@"C:\Users\me\Desktop\varFile.csv")), but I'm failing. Happy to write directly. – Benjamin Levy Nov 03 '16 at 12:47

0 Answers0