public class ConvertToEntity<T> where T : new()
{
public static T ConvertDataRowToModel(DataRow row)
{
T t = new T();
Type type = t.GetType();
foreach (PropertyInfo pi in type.GetProperties())
{
if (row.Table.Columns.Contains(pi.Name))
{
if (!pi.CanWrite)
continue;
object rowValue = row[pi.Name];
if (rowValue.GetType() == typeof(Int64))
{
rowValue = Convert.ToInt32(rowValue);
}
if (rowValue != DBNull.Value)
{
try
{
pi.SetValue(t, rowValue, null);
}
catch (Exception e)
{
LogHelper.Error(null, e.Message);
throw e;
}
}
}
}
return t;
}
}
{
public static T ConvertDataRowToModel(DataRow row)
{
T t = new T();
Type type = t.GetType();
foreach (PropertyInfo pi in type.GetProperties())
{
if (row.Table.Columns.Contains(pi.Name))
{
if (!pi.CanWrite)
continue;
object rowValue = row[pi.Name];
if (rowValue.GetType() == typeof(Int64))
{
rowValue = Convert.ToInt32(rowValue);
}
if (rowValue != DBNull.Value)
{
try
{
pi.SetValue(t, rowValue, null);
}
catch (Exception e)
{
LogHelper.Error(null, e.Message);
throw e;
}
}
}
}
return t;
}
}