惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

C
CXSECURITY Database RSS Feed - CXSecurity.com
Stack Overflow Blog
Stack Overflow Blog
月光博客
月光博客
T
Threat Research - Cisco Blogs
小众软件
小众软件
有赞技术团队
有赞技术团队
酷 壳 – CoolShell
酷 壳 – CoolShell
Apple Machine Learning Research
Apple Machine Learning Research
C
Cyber Attacks, Cyber Crime and Cyber Security
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
T
Tailwind CSS Blog
Cisco Talos Blog
Cisco Talos Blog
V
V2EX
博客园 - 【当耐特】
C
Cybersecurity and Infrastructure Security Agency CISA
Hugging Face - Blog
Hugging Face - Blog
The Cloudflare Blog
The Last Watchdog
The Last Watchdog
Simon Willison's Weblog
Simon Willison's Weblog
T
Threatpost
S
Secure Thoughts
O
OpenAI News
P
Proofpoint News Feed
S
SegmentFault 最新的问题
Forbes - Security
Forbes - Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Application and Cybersecurity Blog
Application and Cybersecurity Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Last Week in AI
Last Week in AI
宝玉的分享
宝玉的分享
Scott Helme
Scott Helme
T
Tenable Blog
A
Arctic Wolf
L
LINUX DO - 热门话题
爱范儿
爱范儿
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
www.infosecurity-magazine.com
www.infosecurity-magazine.com
V
Visual Studio Blog
Hacker News: Ask HN
Hacker News: Ask HN
Hacker News - Newest:
Hacker News - Newest: "LLM"
腾讯CDC
博客园 - Franky
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
博客园_首页
雷峰网
雷峰网
IT之家
IT之家
PCI Perspectives
PCI Perspectives
L
LINUX DO - 最新话题
H
Heimdal Security Blog

博客园 - 雨过流痕

net webservice如何调出测试调用 转载:Android 2.3.3 近场通信NFC介绍 用带无线网卡笔记本组建无线局域网 Windows Mobile 开发系列文章收藏 - Device Emulator 基于.NET的开源GIS项目 什么是程序的API? 什么是Windows API? 城管通解决方案成功案例 (转)制作web安装程序 .NET程序运行原理 邮箱 服务器地址 最新发布的Visual Studio 2008 Service Pack 1和.NET Framework 3.5 Service Pack 1 [转]VS2005,VS2008快捷键大全 手机短信发送接收流程 Service Unavailable 问题及解决方法 IIS优化 为Web服务器减负 基于.NET CompactFramework的九宫格控件(附源码) C#断点下载(转帖) C#基础概念二十五问
C#反射,根据反射将数据库查询数据和实体类绑定,并为实体类赋值
雨过流痕 · 2011-01-12 · via 博客园 - 雨过流痕

/****************数据库脚本*************** 
* 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 
}