Get model type by name

  • Thread starter Thread starter FireScroll
  • Start date Start date
F

FireScroll

Guest
Dear friends,

I have this model class:

namespace gicaic_ws.models
{
public class modelos_cab
{
public string style { get; set; }
public string style_customer { get; set; }
public string season { get; set; }
public string season_description_pt { get; set; }
}

public class modelos_lin
{
public string style { get; set; }
public string colour_code { get; set; }
public string colour_code_customer { get; set; }
public string barcode { get; set; }
public string description1_pt { get; set; }
}
}



And I'm creating the object and adding using this method:

using (DataTable dt = new DataTable())
using (SqlConnection cn = new SqlConnection(tools_ws.get_connection_string()))
using (SqlCommand cmd = new SqlCommand())
{
if (cn.State == ConnectionState.Closed) cn.Open();
cmd.Connection = cn;
cmd.CommandText += "";
using (SqlDataAdapter da = new SqlDataAdapter(cmd)) da.Fill(dt);
List<models.modelos_cab> m = new List<models.modelos_cab>(); //ALTERAR O MODEL ***********

var c_names = dt.Columns.Cast<DataColumn>().Select(c => c.ColumnName.ToLower()).ToList();
var p = typeof(models.modelos_cab).GetProperties(); //ALTERAR O MODEL ***********
m = dt.AsEnumerable().Select(row =>
{
var o = Activator.CreateInstance<models.modelos_cab>(); //ALTERAR O MODEL ***********
foreach (var pr in p) if (c_names.Contains(pr.Name.ToLower())) try { pr.SetValue(o, row[pr.Name]); } catch (Exception ex) { }
return o;
}).ToList();
}


How can I can I pass the model as a string, and get it by it's name? Something like this:

string model_name = "models.modelos_cab";

List<getModelByName(model_name)> m = new List<getModelByName(model_name)>();

var p = typeof(getModelByName(model_name)).GetProperties();

var o = Activator.CreateInstance<getModelByName(model_name)>();


Is this anyway possible?

Thank you in advance!

Continue reading...
 
Back
Top