Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
6d16ac8f
D
Dapper
项目概览
int
/
Dapper
大约 1 年 前同步成功
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
Dapper
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
6d16ac8f
编写于
10月 25, 2011
作者:
S
Sam Saffron
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add struct support to dapper
上级
e5102341
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
72 addition
and
10 deletion
+72
-10
Dapper/SqlMapper.cs
Dapper/SqlMapper.cs
+49
-10
Tests/Tests.cs
Tests/Tests.cs
+23
-0
未找到文件。
Dapper/SqlMapper.cs
浏览文件 @
6d16ac8f
...
...
@@ -926,10 +926,14 @@ private static CacheInfo GetCacheInfo(Identity identity)
}
#endif
if
(
type
.
IsClass
&&
type
!=
typeof
(
string
)
&&
type
!=
typeof
(
byte
[])
&&
type
!=
typeof
(
System
.
Data
.
Linq
.
Binary
))
if
(
(
type
.
IsClass
&&
type
!=
typeof
(
string
)
&&
type
!=
typeof
(
byte
[])
&&
type
!=
typeof
(
System
.
Data
.
Linq
.
Binary
))
||
(
type
.
IsValueType
&&
!
type
.
IsPrimitive
&&
!(
type
.
IsGenericType
&&
type
.
GetGenericTypeDefinition
()
==
typeof
(
Nullable
<>)))
)
{
return
Get
Class
Deserializer
(
type
,
reader
,
startBound
,
length
,
returnNullIfFirstMissing
);
return
Get
Type
Deserializer
(
type
,
reader
,
startBound
,
length
,
returnNullIfFirstMissing
);
}
return
GetStructDeserializer
(
type
,
startBound
);
}
...
...
@@ -1437,7 +1441,7 @@ static List<FieldInfo> GetSettableFields(Type t)
/// <param name="length"></param>
/// <param name="returnNullIfFirstMissing"></param>
/// <returns></returns>
public
static
Func
<
IDataReader
,
object
>
Get
Class
Deserializer
(
public
static
Func
<
IDataReader
,
object
>
Get
Type
Deserializer
(
#if CSHARP30
Type
type
,
IDataReader
reader
,
int
startBound
,
int
length
,
bool
returnNullIfFirstMissing
#else
...
...
@@ -1445,11 +1449,11 @@ static List<FieldInfo> GetSettableFields(Type t)
#endif
)
{
var
dm
=
new
DynamicMethod
(
string
.
Format
(
"Deserialize{0}"
,
Guid
.
NewGuid
()),
type
,
new
[]
{
typeof
(
IDataReader
)
},
true
);
var
dm
=
new
DynamicMethod
(
string
.
Format
(
"Deserialize{0}"
,
Guid
.
NewGuid
()),
type
.
IsValueType
?
typeof
(
object
)
:
type
,
new
[]
{
typeof
(
IDataReader
)
},
true
);
var
il
=
dm
.
GetILGenerator
();
il
.
DeclareLocal
(
typeof
(
int
));
il
.
DeclareLocal
(
type
);
var
emptyLocal
=
il
.
DeclareLocal
(
type
);
bool
haveEnumLocal
=
false
;
il
.
Emit
(
OpCodes
.
Ldc_I4_0
);
il
.
Emit
(
OpCodes
.
Stloc_0
);
...
...
@@ -1485,7 +1489,20 @@ static List<FieldInfo> GetSettableFields(Type t)
il
.
BeginExceptionBlock
();
// stack is empty
il
.
Emit
(
OpCodes
.
Newobj
,
type
.
GetConstructor
(
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
,
null
,
Type
.
EmptyTypes
,
null
));
// stack is now [target]
if
(
type
.
IsValueType
)
{
il
.
Emit
(
OpCodes
.
Ldloca
,
emptyLocal
);
il
.
Emit
(
OpCodes
.
Initobj
,
type
);
il
.
Emit
(
OpCodes
.
Ldloca
,
emptyLocal
);
}
else
{
il
.
Emit
(
OpCodes
.
Newobj
,
type
.
GetConstructor
(
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
,
null
,
Type
.
EmptyTypes
,
null
));
}
// stack is now [target]
bool
first
=
true
;
var
allDone
=
il
.
DefineLabel
();
foreach
(
var
item
in
setters
)
...
...
@@ -1502,7 +1519,6 @@ static List<FieldInfo> GetSettableFields(Type t)
il
.
Emit
(
OpCodes
.
Stloc_0
);
// stack is now [target][target][reader][index]
il
.
Emit
(
OpCodes
.
Callvirt
,
getItem
);
// stack is now [target][target][value-as-object]
Type
memberType
=
item
.
Property
!=
null
?
item
.
Property
.
Type
:
item
.
Field
.
FieldType
;
if
(
memberType
==
typeof
(
char
)
||
memberType
==
typeof
(
char
?))
...
...
@@ -1580,7 +1596,14 @@ static List<FieldInfo> GetSettableFields(Type t)
}
if
(
item
.
Property
!=
null
)
{
il
.
Emit
(
OpCodes
.
Callvirt
,
item
.
Property
.
Setter
);
// stack is now [target]
if
(
type
.
IsValueType
)
{
il
.
Emit
(
OpCodes
.
Call
,
item
.
Property
.
Setter
);
// stack is now [target]
}
else
{
il
.
Emit
(
OpCodes
.
Callvirt
,
item
.
Property
.
Setter
);
// stack is now [target]
}
}
else
{
...
...
@@ -1607,7 +1630,14 @@ static List<FieldInfo> GetSettableFields(Type t)
first
=
false
;
index
+=
1
;
}
il
.
Emit
(
OpCodes
.
Stloc_1
);
// stack is empty
if
(
type
.
IsValueType
)
{
il
.
Emit
(
OpCodes
.
Pop
);
}
else
{
il
.
Emit
(
OpCodes
.
Stloc_1
);
// stack is empty
}
il
.
MarkLabel
(
allDone
);
il
.
BeginCatchBlock
(
typeof
(
Exception
));
// stack is Exception
il
.
Emit
(
OpCodes
.
Ldloc_0
);
// stack is Exception, index
...
...
@@ -1617,7 +1647,16 @@ static List<FieldInfo> GetSettableFields(Type t)
il
.
Emit
(
OpCodes
.
Stloc_1
);
// to make it verifiable
il
.
EndExceptionBlock
();
il
.
Emit
(
OpCodes
.
Ldloc_1
);
// stack is empty
if
(
type
.
IsValueType
)
{
il
.
Emit
(
OpCodes
.
Ldloc
,
emptyLocal
);
// stack is [rval]
il
.
Emit
(
OpCodes
.
Box
,
type
);
}
else
{
il
.
Emit
(
OpCodes
.
Ldloc_1
);
// stack is [rval]
}
il
.
Emit
(
OpCodes
.
Ret
);
return
(
Func
<
IDataReader
,
object
>)
dm
.
CreateDelegate
(
typeof
(
Func
<
IDataReader
,
object
>));
...
...
Tests/Tests.cs
浏览文件 @
6d16ac8f
...
...
@@ -16,6 +16,28 @@ class Tests
{
SqlConnection
connection
=
Program
.
GetOpenConnection
();
struct
Car
{
public
enum
TrapEnum
:
int
{
A
=
1
,
B
=
2
}
public
string
Name
;
public
int
Age
{
get
;
set
;
}
public
TrapEnum
Trap
{
get
;
set
;
}
}
public
void
TestStructs
()
{
var
car
=
connection
.
Query
<
Car
>(
"select 'Ford' Name, 21 Age, 2 Trap"
).
First
();
car
.
Age
.
IsEqualTo
(
21
);
car
.
Name
.
IsEqualTo
(
"Ford"
);
((
int
)
car
.
Trap
).
IsEqualTo
(
2
);
}
public
void
SelectListInt
()
{
connection
.
Query
<
int
>(
"select 1 union all select 2 union all select 3"
)
...
...
@@ -1111,5 +1133,6 @@ public void TestAppendingAList()
result
[
1
].
IsEqualTo
(
2
);
result
[
2
].
IsEqualTo
(
3
);
}
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录