David Johnson
David Johnson

Reputation: 419

Creating Class at Runtime

I have a sql database that is written to on a daily basis by some c# code, which contains stock information on a particular date.

Fields: StockCode, NumberOfHolders, Value, Date

Each day there is a new record added to the database with a date stamp.

I need to be able to create a list which is in the following format;

         date1   date2   date3
 stock1   100     104      110
 stock2   105     100      99 
 stock3   150     150      80 

etc.

However, this list would seems to have to be dynamic, as the fields would change, depending on the number of dates in the system for each stock.

So I could not use a class such as;

private class Stocks: ExcelReport
{
  public String StockCode { get; set; }
  public Double TotalQtyHeld { get; set; }
  public Double TotalValueOfStock { get; set; }
  public DateTime Date { get; set; }
}

Any help appreciated.

Thanks

Upvotes: 0

Views: 228

Answers (4)

Lzh
Lzh

Reputation: 3635

I'm not sure if I understand so please forgive me if my answer is trivial to your requirements. Don't you just need a list of values? Class generation sounds like a genius solution, but it's too complicated for the task.

Create a list of date/stock information values (or objects). You can use a Tuple<Date, double> as a Type or create your own custom class:

class DataValue
{
    public DateTime Date { get; set; }
    public double Value { get; set; }
}

Then just use a List<DataValue>

Upvotes: 1

Mark Alicz
Mark Alicz

Reputation: 387

not saying this is the best option for you to use however you can compile your own classes at run time and them load them. Below is an example of how to compile text into a DLL.

        CSharpCodeProvider codeProvider = new CSharpCodeProvider();

        System.CodeDom.Compiler.CompilerParameters parameters = new CompilerParameters();
        parameters.GenerateExecutable = false;
        parameters.GenerateInMemory = false;
        parameters.WarningLevel = 3;
        parameters.CompilerOptions = "/optimize";
        parameters.OutputAssembly = "C:\\test\\test.dll";
        parameters.ReferencedAssemblies.Add("Microsoft.CSharp.dll");
        parameters.ReferencedAssemblies.Add("System.dll");
        parameters.ReferencedAssemblies.Add("System.Core.dll");
        parameters.ReferencedAssemblies.Add("System.Data.dll");
        parameters.ReferencedAssemblies.Add("System.Data.DataSetExtensions.dll");
        parameters.ReferencedAssemblies.Add("System.XML.dll");
        parameters.ReferencedAssemblies.Add("System.XML.Linq.dll");

        CompilerResults results = codeProvider.CompileAssemblyFromSource(parameters, _BaseCode.ToArray());

        if (results.Errors.Count > 0)
        {
            LogError(results.Errors[0].ErrorText, "Error Compiling", null, "", ErrorLevel.Critical);
            throw new Exception("Error Compiling..");
        }

The above code is just a snip of a larger method I use to compile code. some variables do not exist etc. Use it as a base and lookup the methods being used. After you compile the code you can load the DLL and create an instance. The code below shows some samples.

System.Reflection.Assembly _A = System.Reflection.Assembly.LoadFile(FileLocation);

return (T)_A.CreateInstance(_ClassName, true, System.Reflection.BindingFlags.CreateInstance, null, Arguments.ToArray(), System.Globalization.CultureInfo.CurrentCulture, null);

Upvotes: 0

sgud
sgud

Reputation: 442

A List of key-value/ tuple can serve your purpose as long as you have 2-3 values, If you need more information go for Jeffery solution.

private class Stocks : ExcelReport
    {
        public String StockCode { get; set; }
        public List<Tuple<DateTime, Double, Double>> StocksValues { get; set; }
    }

Upvotes: 0

Jeffrey Lott
Jeffrey Lott

Reputation: 7419

Couldn't you create a class that can hold the dates and values like this:

private class StockValue
{
  public Double TotalQtyHeld { get; set; }
  public Double TotalValueOfStock { get; set; }
  public DateTime Date { get; set; }
}

private class Stocks: ExcelReport
{
  public String StockCode { get; set; }
  public IList<StockValue> Values {get;set;}
}

Upvotes: 3

Related Questions