DataRow dr=dt.Rows[0]; // //获取实体类的类型 Type type= Assembly.GetExecutingAssembly().GetType("Entities."+dt.TableName); //调用带参数的构造获取对象 object obj= Activator.CreateInstance(type, new object[] { dr});
------解决方案--------------------
C# code
public IEntity GetEntity(IEntity entity)//目前只能根据ID查询
{
try
{
Type type = entity.GetType();
PropertyInfo[] list = type.GetProperties();
string sql = "select top 1 * from " + type.Name + " where ";
foreach (PropertyInfo info in list)
{
if (info.Name == GetKeyName(type.Name))
{
sql += info.Name + "=" + info.GetValue(entity, null);
break;
}
}
DataTable dt = Query(sql);
if (dt.Rows.Count > 0)
{
for (int j = 0; j < list.Length; j++)
{
if (list[j].Name == GetKeyName(type.Name) ||
dt.Rows[0][list[j].Name] is DBNull)
{
continue;
}
string columnName = list[j].Name;
object value = dt.Rows[0][columnName];
list[j].SetValue(entity, value, null);
}
return entity;
}
else
{
return null;
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
}
------解决方案--------------------
------解决方案-------------------- public static IList<T> FillList<T>(System.Data.IDataReader reader) { IList<T> lst= new List<T>(); while (reader.Read()) { T RowInstance = Activator.CreateInstance<T>(); foreach (PropertyInfo Property in typeof(T).GetProperties())