全文摘自http://www.cnblogs.com/g1mist/p/3227290.html,很好的一個(gè)實(shí)例。
反射提供了封裝程序集、模塊和類型的對(duì)象。您可以使用反射動(dòng)態(tài)地創(chuàng)建類型的實(shí)例,將類型綁定到現(xiàn)有對(duì)象,或從現(xiàn)有對(duì)象中獲取類型。然后,可以調(diào)用類型的方法或訪問其字段和屬性。
1.先建立實(shí)體類
用戶實(shí)體類:
1
2
3
4
5
6
7
8
9
|
public
class
User
????
{
????????
public
int
id {
get
;
set
; }
????????
public
string
UserName {
get
;
set
; }
????????
public
string
Password {
get
;
set
; }
????????
public
int
Age {
get
;
set
; }
????????
public
string
PhoneNumber {
get
;
set
; }
????????
public
string
Address {
get
;
set
; }
????
}
|
書籍實(shí)體類:
1
2
3
4
5
6
7
8
|
public
class
Book
???
{
???????
public
int
id {
set
;
get
; }
???????
public
string
BookName {
get
;
set
; }
???????
public
string
ISBN {
set
;
get
; }
???????
public
string
Author {
set
;
get
; }
???????
public
double
Price {
set
;
get
; }
???
}
|
2.通過反射技術(shù)來(lái)生成Insert語(yǔ)句(舉個(gè)例子而已,只生成Insert語(yǔ)句)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
|
??
/// <summary>
??
/// 泛型方法,反射生成SQLInsert語(yǔ)句
/// </summary>
??
/// <typeparam name="T">實(shí)體類型</typeparam>
??
/// <param name="entity">實(shí)體對(duì)象</param>
??
/// <returns></returns>
??
public
string
CreateInsertSQL<T>(T entity)
??
{
??????
//1.先獲取實(shí)體的類型描述
??????
Type type = entity.GetType();
??????
//2.獲得實(shí)體的屬性集合
??????
PropertyInfo[] props = type.GetProperties();
?
??????
//實(shí)例化一個(gè)StringBuilder做字符串的拼接
??
StringBuilder sb =
new
StringBuilder();
?
??????
sb.Append(
"insert into "
+ type.Name +
" ("
);
?
??????
//3.遍歷實(shí)體的屬性集合
??????
foreach
(PropertyInfo prop
in
props)
??????
{
??????????
//4.將屬性的名字加入到字符串中
?????
sb.Append(prop.Name +
","
);
??????
}
??????
//**去掉最后一個(gè)逗號(hào)
???
sb.Remove(sb.Length - 1, 1);
??????
sb.Append(
" ) values("
);
?
??????
//5.再次遍歷,形成參數(shù)列表"(@xx,@xx@xx)"的形式
??????
foreach
(PropertyInfo prop
in
props)
??????
{
??????????
sb.Append(
"@"
+ prop.Name +
","
);
??????
}
??????
//**去掉最后一個(gè)逗號(hào)
??????
sb.Remove(sb.Length - 1, 1);
??????
sb.Append(
")"
);
?
??????
return
sb.ToString();
??
}
|
3.測(cè)試
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
class
Program
????
{
????????
static
void
Main(
string
[] args)
????????
{
//調(diào)用ReflationCreateSQL類中的CreateInsertSQL方法??????????
????????
string
sql =
new
ReflationCreateSQL().CreateInsertSQL(
new
User());
????????????
string
sql1 =
new
ReflationCreateSQL().CreateInsertSQL(
new
Book());
?
????????????
Console.WriteLine(sql.ToString());
????????????
Console.WriteLine(sql1.ToString());
?
????????????
Console.WriteLine(
"Press any key to continue . . ."
);
????????????
Console.ReadLine();
????????
}
????
}
|
結(jié)果:
但是,我們發(fā)現(xiàn)id是主鍵,假設(shè)id是自增長(zhǎng)的,我們生成的SQL(Insert)語(yǔ)句中就不應(yīng)該有id,在這里我用自定義Attribute的方法來(lái)解決這個(gè)問題。
4.先新建一個(gè)類,繼承Attribute類
1
2
3
4
|
public
class
KEYAttribute : Attribute
{
?
}
|
這個(gè)類僅此而已就夠了。
5.在實(shí)體類中的id這個(gè)字段上加上[KEY]標(biāo)記
1
2
3
4
5
6
7
8
9
|
public
class
Book
????
{
????????
[KEY]
????????
public
int
id {
set
;
get
; }
????????
public
string
BookName {
get
;
set
; }
????????
public
string
ISBN {
set
;
get
; }
????????
public
string
Author {
set
;
get
; }
????????
public
double
Price {
set
;
get
; }
????
}
|
?
1
2
3
4
5
6
7
8
9
10
|
public
class
User
???
{
???????
[KEY]
???????
public
int
id {
get
;
set
; }
???????
public
string
UserName {
get
;
set
; }
???????
public
string
Password {
get
;
set
; }
???????
public
int
Age {
get
;
set
; }
???????
public
string
PhoneNumber {
get
;
set
; }
???????
public
string
Address {
get
;
set
; }
???
}
|
6.加好標(biāo)記之后,我們只需要這CreateInsertSQL<T>(T entity)這個(gè)方法中的兩個(gè)foreach循環(huán)體中加一些判斷即可
1
2
3
4
5
6
7
8
9
10
11
12
|
foreach
(PropertyInfo prop
in
props)
???????????
{
???????????????
//獲取用戶自定義標(biāo)記集合
??????????
object
[] attrs = prop.GetCustomAttributes(
typeof
(KEYAttribute),
true
);
???????????????
//如果屬性上有自定義標(biāo)記KEYAttribute,退出本次循環(huán)
???????????
if
(attrs.Length > 0)
???????????????
{
???????????????????
continue
;
???????????????
}
???????????????
//將屬性的名字加入到字符串中
??????????
sb.Append(prop.Name +
","
);
???????????
}
|
1
2
3
4
5
6
7
8
9
|
foreach
(PropertyInfo prop
in
props)
????????????
{
????????????????
object
[] attrs = prop.GetCustomAttributes(
typeof
(KEYAttribute),
true
);
????????????????
if
(attrs.Length > 0)
????????????????
{
????????????????????
continue
;
????????????????
}
????????????????
sb.Append(
"@"
+ prop.Name +
","
);
????????????
}
|
7.測(cè)試
更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號(hào)聯(lián)系: 360901061
您的支持是博主寫作最大的動(dòng)力,如果您喜歡我的文章,感覺我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對(duì)您有幫助就好】元
