本文來自:http://www.cnblogs.com/mrchenzh/archive/2010/05/31/1747937.html
/*****************************************
* 說明:利用反射將數據庫查詢的內容自動綁定
* 到實體類
*
* 時間:1:49 2009-9-19
*
* 程序員:王文壯
* ***************************************/
/****************數據庫腳本***************
* create database MySchool
* go
* use MySchool
* go
* create table Student
* (
* ID int identity primary key,
* Name varchar(10)
* )
* ****************************************/
using System;
using System.Reflection;
using System.Data.SqlClient;
using System.Data;
using System.Collections.Generic;
namespace ReflectionDemo
{
#region Main
class Program
{
static void Main(string[] args)
{
DataSet ds = new DataSet();
#region 連接數據庫構建DataSet
//SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=MySchool;Integrated Security=True");
//SqlDataAdapter objAdapter = new SqlDataAdapter("Select * from student", con);
//objAdapter.Fill(ds);
#endregion
#region 手動構建DataSet
DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Name");
DataRow row = dt.NewRow();
row["ID"] = 1;
row["Name"] = "灰太狼";
dt.Rows.Add(row);
ds.Tables.Add(dt);
#endregion
List<Student> students = new List<Student>();
foreach (DataRow dataRow in ds.Tables[0].Rows)
{
Student stu = new Student();
Utility.ConvertToEntity(stu, row);
students.Add(stu);
}
foreach (Student student in students)
{
Console.WriteLine(student.Name);
}
}
}
#endregion
#region 實體類
/// <summary>
/// 實體類,需要在屬性
/// 上添加自定義特性
/// 每個實體類對應數據表里
/// 的一個字段,注意,自定義特性里的參數
/// 一定要和數據表里的字段一一對應,
/// 否則就反射不到了!
/// </summary>
public class Student
{
[DataContextAttribute("ID")]
public int ID { get; set; }
[DataContext("Name")]
public string Name { get; set; }
}
#endregion
#region 自定義特性
/// <summary>
/// 自定義特性
/// </summary>
[AttributeUsage(AttributeTargets.Property)]
public class DataContextAttribute : Attribute
{
/// <summary>
/// 自定義特性
/// </summary>
/// <param name="fieldName">數據表字段名稱</param>
public DataContextAttribute(string property) { this.Property = property; }
/// <summary>
/// 數據表字段屬性(實體屬性)
/// </summary>
public string Property { get; set; }
}
#endregion
#region 反射
public class Utility
{
/// <summary>
/// 將DataRow轉換成實體
/// </summary>
/// <param name="obj">實體</param>
/// <param name="row">數據表一行數據</param>
public static void ConvertToEntity(object obj, DataRow row)
{
///得到obj的類型
Type type = obj.GetType();
///返回這個類型的所有公共屬性
PropertyInfo[] infos = type.GetProperties();
///循環公共屬性數組
foreach (PropertyInfo info in infos)
{
///返回自定義屬性數組
object[] attributes = info.GetCustomAttributes(typeof(DataContextAttribute), false);
///將自定義屬性數組循環
foreach (DataContextAttribute attribute in attributes)
{
///如果DataRow里也包括此列
if (row.Table.Columns.Contains(attribute.Property))
{
///將DataRow指定列的值賦給value
object value = row[attribute.Property];
///如果value為null則返回
if (value == DBNull.Value) continue;
///將值做轉換
if (info.PropertyType.Equals(typeof(string)))
{
value = row[attribute.Property].ToString();
}
else if (info.PropertyType.Equals(typeof(int)))
{
value = Convert.ToInt32(row[attribute.Property]);
}
else if (info.PropertyType.Equals(typeof(decimal)))
{
value = Convert.ToDecimal(row[attribute.Property]);
}
else if (info.PropertyType.Equals(typeof(DateTime)))
{
value = Convert.ToDateTime(row[attribute.Property]);
}
else if (info.PropertyType.Equals(typeof(double)))
{
value = Convert.ToDouble(row[attribute.Property]);
}
else if (info.PropertyType.Equals(typeof(bool)))
{
value = Convert.ToBoolean(row[attribute.Property]);
}
///利用反射自動將value賦值給obj的相應公共屬性
info.SetValue(obj, value, null);
}
}
}
}
}
#endregion
}