32 lines
895 B
C#
32 lines
895 B
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace JinYuan.DAL.Models
|
|
{
|
|
public static class ModelExtension
|
|
{
|
|
public static T ToModel<T>(this System.Data.DataRow row) where T : class, new()
|
|
{
|
|
if (row == null) return null;
|
|
T model = new T();
|
|
var type = typeof(T);
|
|
var properties = type.GetProperties();
|
|
foreach (var prop in properties)
|
|
{
|
|
if (row.Table.Columns.Contains(prop.Name))
|
|
{
|
|
var value = row[prop.Name];
|
|
if (value != DBNull.Value)
|
|
{
|
|
prop.SetValue(model, Convert.ChangeType(value, prop.PropertyType));
|
|
}
|
|
}
|
|
}
|
|
return model;
|
|
}
|
|
}
|
|
}
|