Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to initialize various type variables in dynamically generated C# code?

I have T4 template where I'd like to generate an .cs file.

I have an array of System.Data.DataColumn that i'd like to use as a private variables in my generated code file.

I'm using ColumnName as variable name, Value as variable value and DataType as variable data type.

I'm thinking on how do I initialize defined variables in this case:

ColumnName = "col1"
ColumnValue = "text"
DatType = System.String

I'd like to see output: private System.String col1 = "text";

Variable definition in T4 template:

private <#= DataType.ToString() #> <#= ColumnName #> = "<=# ColumnValue #>"

I'm thinking about writing helper method, that will return variable initialization string for common data types. Something like:

public string ValueInitializerString
        {
            get
            {

                string valueString;
                if (this.DataType == typeof(int))
                {
                    valueString = this.Value.ToString();
                }
                else if (this.DataType == typeof(string))
                {
                    valueString = string.Format("\"{0}\"", this.Value);
                }
                else if (this.DataType == typeof(DateTime))
                {
                    DateTime dateTime = (DateTime)this.Value;

                    valueString = string.Format("new DateTime({0}, {1}, {2}, {3}, {4}, {5}, {6})",
                                                              dateTime.Year, dateTime.Month, dateTime.Day, dateTime.Hour, dateTime.Minute, dateTime.Second, dateTime.Millisecond);
                }
                else
                {
                    throw new Exception(string.Format("Data type {0} not supported. ", this.DataType));
                }
                return valueString;
            }
        } 

If someone did something similar, could you advice if this is a good idea or it could be done in more convenient way? Maybe I should read something?

like image 700
Sergejs Avatar asked Apr 23 '13 10:04

Sergejs


2 Answers

This should work fine, although I would make it a generic class. Something like

internal class DataType<T>
{
      public string Name {get;set}
      public T Value {get;set;}
      public Type Type
      {
          get { return typeof(T); }
      }

      public string VariableDefinition()
      {
          /* Construct string */
      }
}

This would more flexible and reusable. Example usage:

<#
var dataType = new DataType<String>()
{
    Name = "col1",
    Value = "text"
};
#>

private <#= dataType.VariableDefinition() #>
like image 89
Shawn South Avatar answered Oct 19 '22 13:10

Shawn South


Hope this works.

Use ExpandoObject to solve your problem. Definition of ExpandoObject as per MSDN :

It represents an object whose members can be dynamically added and removed at run time.

To set datatype and value at runtime , use Convert.ChangeType method. This creates an object of same type and value as you provide.

Reference for ExpandoObject: https://blogs.msdn.microsoft.com/csharpfaq/2009/09/30/dynamic-in-c-4-0-introducing-the-expandoobject/

Reference for Convert.ChangeType : https://msdn.microsoft.com/en-us/library/system.convert.changetype(v=vs.110).aspx

So create properties dynamically using ExpandoObject and create datatype dynamically using Convert.ChangeType.

Code :

class Program
{
        static void Main(string[] args)
        {
        // I have used hardcoded values representing database values
        var dataTable = new DataTable();
        dataTable.Columns.Add(new DataColumn("Column1"));
        dataTable.Columns.Add(new DataColumn("Column2"));
        var row = dataTable.NewRow();
        row[0] = 1;
        row[1] = "Test Value";
        dataTable.Rows.Add(row);

        // This list below contains properties - variables , with same datatype and value
        dynamic parentList = new List<dynamic>();

        var rowOne = dataTable.Rows[0]; 
        for (int i = 0; i < dataTable.Columns.Count; i++)
        {
            dynamic child= new ExpandoObject();

            child.Property = Convert.ChangeType(row[i], row[i].GetType());
            parentList.Add(child); 
        }
    }

}
like image 1
SunilA Avatar answered Oct 19 '22 13:10

SunilA