I have the next two methods, works to implement bind the results of multiple store proeceadures into models, my codes:
public static List<T> SPReader<T>(string procedure, TokenSessionSeus tokenSesion, params object[] parameteres) where T : new()
{
var model = new List<T>();
var type = typeof(T);
var props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var conection = OpenConnection(procedure, tokenSesion, parameteres);
DataAccess consultar = new DataAccess();
DataSet ds = consultar.GetData(conection);
var consulta = new DataSources<ModelosEquivalentes>();
#region
var t = typeof(T);
var fieldValues = t.GetProperties(BindingFlags.Public | BindingFlags.Instance);
var tableModel = new List<TableModel>();
foreach (var field in fieldValues)
{
var attr = (MetaAttribute[])field.GetCustomAttributes(typeof(MetaAttribute), false);
if (!attr.Any())
break;
var typesToCast = (field.PropertyType.IsGenericType)
? field.PropertyType.GetGenericArguments()[0].Name
: field.PropertyType.Name;
tableModel.Add(new TableModel
{
ModelName = field.Name,
ModelType = typesToCast
});
}
#endregion
for (var i = 0; i <= tableModel.Count() - 1; i++)
{
Type currentType = Type.GetType("ApiCRMDrive.Models." + tableModel[i].ModelType);
MethodInfo method = typeof(GestionVendedor).GetMethod("ToModel");
MethodInfo generic = method.MakeGenericMethod(currentType);
var result = generic.Invoke(null, new object[] { ds.Tables[0] });
object o = Activator.CreateInstance(currentType);
PropertyInfo propertyInfo = Type.GetType("ApiCRMDrive.Models." + type.Name).GetProperty(tableModel[i].ModelName);
propertyInfo.SetValue(o, Convert.ChangeType(result, currentType), null);
}
//var result = ToModel<EmpresasInfoModel>(ds.Tables[1]);
return model;
}
public static List<T> ToModel<T>(this DataTable dt)
{
List<string> columns = (from DataColumn dc in dt.Columns select dc.ColumnName).ToList();
var fields = typeof(T).GetFields();
var properties = typeof(T).GetProperties();
List<T> lst = new List<T>();
foreach (DataRow dr in dt.Rows)
{
var ob = Activator.CreateInstance<T>();
foreach (var fieldInfo in fields.Where(fieldInfo => columns.Contains(fieldInfo.Name)))
{
fieldInfo.SetValue(ob, !dr.IsNull(fieldInfo.Name) ? dr[fieldInfo.Name] : fieldInfo.FieldType.IsValueType ? Activator.CreateInstance(fieldInfo.FieldType) : null);
}
foreach (var propertyInfo in properties.Where(propertyInfo => columns.Contains(propertyInfo.Name)))
{
propertyInfo.SetValue(ob, !dr.IsNull(propertyInfo.Name) ? dr[propertyInfo.Name] : propertyInfo.PropertyType.IsValueType ? Activator.CreateInstance(propertyInfo.PropertyType) : null);
}
lst.Add(ob);
}
return lst;
}
Ok, whats the problem then?
At the moment to assign a value to the property with:
Type currentType = Type.GetType("ApiCRMDrive.Models." + tableModel[i].ModelType);
MethodInfo method = typeof(GestionVendedor).GetMethod("ToModel");
MethodInfo generic = method.MakeGenericMethod(currentType);
var result = generic.Invoke(null, new object[] { ds.Tables[0] });
object o = Activator.CreateInstance(currentType);
PropertyInfo propertyInfo = Type.GetType("ApiCRMDrive.Models." + type.Name).GetProperty(tableModel[i].ModelName);
propertyInfo.SetValue(o, Convert.ChangeType(result, currentType), null); //HERE IS WHERE THE ERROR HAPPENS
For some reason, in the line to assign the result, I get the error:
Object must implement Iconvertible
Aucun commentaire:
Enregistrer un commentaire