Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
b73acfce
D
Dapper
项目概览
int
/
Dapper
11 个月 前同步成功
通知
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,发现更多精彩内容 >>
提交
b73acfce
编写于
3月 02, 2012
作者:
V
vosen
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Add support for types with constructors matching the types pulled by data reader.
上级
f97241e5
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
71 addition
and
37 deletion
+71
-37
Dapper/SqlMapper.cs
Dapper/SqlMapper.cs
+66
-24
Tests/Tests.cs
Tests/Tests.cs
+5
-13
未找到文件。
Dapper/SqlMapper.cs
浏览文件 @
b73acfce
...
@@ -1497,6 +1497,7 @@ static List<FieldInfo> GetSettableFields(Type t)
...
@@ -1497,6 +1497,7 @@ static List<FieldInfo> GetSettableFields(Type t)
int
index
=
startBound
;
int
index
=
startBound
;
ConstructorInfo
specializedConstructor
=
null
;
if
(
type
.
IsValueType
)
if
(
type
.
IsValueType
)
{
{
il
.
Emit
(
OpCodes
.
Ldloca_S
,
(
byte
)
1
);
il
.
Emit
(
OpCodes
.
Ldloca_S
,
(
byte
)
1
);
...
@@ -1504,19 +1505,39 @@ static List<FieldInfo> GetSettableFields(Type t)
...
@@ -1504,19 +1505,39 @@ static List<FieldInfo> GetSettableFields(Type t)
}
}
else
else
{
{
var
ctor
=
type
.
GetConstructor
(
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
,
null
,
Type
.
EmptyTypes
,
null
);
var
types
=
new
Type
[
length
-
startBound
];
if
(
ctor
==
null
)
for
(
int
i
=
startBound
;
i
<
startBound
+
length
;
i
++)
{
{
throw
new
InvalidOperationException
(
"A parameterless default constructor is required to allow for dapper materialization"
);
types
[
i
-
startBound
]
=
reader
.
GetFieldType
(
i
);
}
var
ctorWithParameters
=
type
.
GetConstructor
(
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
,
null
,
types
,
null
);
if
(
ctorWithParameters
!=
null
)
{
var
ctorParams
=
ctorWithParameters
.
GetParameters
();
for
(
int
i
=
0
;
i
<
ctorParams
.
Length
;
i
++)
{
if
(!
String
.
Equals
(
ctorParams
[
i
].
Name
,
names
[
i
],
StringComparison
.
OrdinalIgnoreCase
))
break
;
specializedConstructor
=
ctorWithParameters
;
}
}
if
(
specializedConstructor
==
null
)
{
var
ctor
=
type
.
GetConstructor
(
BindingFlags
.
Instance
|
BindingFlags
.
Public
|
BindingFlags
.
NonPublic
,
null
,
Type
.
EmptyTypes
,
null
);
if
(
ctor
==
null
)
{
throw
new
InvalidOperationException
(
"A parameterless default constructor is required to allow for dapper materialization"
);
}
il
.
Emit
(
OpCodes
.
Newobj
,
ctor
);
il
.
Emit
(
OpCodes
.
Stloc_1
);
}
}
il
.
Emit
(
OpCodes
.
Newobj
,
ctor
);
il
.
Emit
(
OpCodes
.
Stloc_1
);
}
}
il
.
BeginExceptionBlock
();
il
.
BeginExceptionBlock
();
if
(
type
.
IsValueType
)
if
(
type
.
IsValueType
)
{
{
il
.
Emit
(
OpCodes
.
Ldloca_S
,
(
byte
)
1
);
// [target]
il
.
Emit
(
OpCodes
.
Ldloca_S
,
(
byte
)
1
);
// [target]
}
else
}
else
if
(
specializedConstructor
==
null
)
{
{
il
.
Emit
(
OpCodes
.
Ldloc_1
);
// [target]
il
.
Emit
(
OpCodes
.
Ldloc_1
);
// [target]
}
}
...
@@ -1529,7 +1550,8 @@ static List<FieldInfo> GetSettableFields(Type t)
...
@@ -1529,7 +1550,8 @@ static List<FieldInfo> GetSettableFields(Type t)
{
{
if
(
item
.
Property
!=
null
||
item
.
Field
!=
null
)
if
(
item
.
Property
!=
null
||
item
.
Field
!=
null
)
{
{
il
.
Emit
(
OpCodes
.
Dup
);
// stack is now [target][target]
if
(
specializedConstructor
==
null
)
il
.
Emit
(
OpCodes
.
Dup
);
// stack is now [target][target]
Label
isDbNullLabel
=
il
.
DefineLabel
();
Label
isDbNullLabel
=
il
.
DefineLabel
();
Label
finishLabel
=
il
.
DefineLabel
();
Label
finishLabel
=
il
.
DefineLabel
();
...
@@ -1587,13 +1609,16 @@ static List<FieldInfo> GetSettableFields(Type t)
...
@@ -1587,13 +1609,16 @@ static List<FieldInfo> GetSettableFields(Type t)
{
{
il
.
Emit
(
OpCodes
.
Newobj
,
memberType
.
GetConstructor
(
new
[]
{
nullUnderlyingType
}));
il
.
Emit
(
OpCodes
.
Newobj
,
memberType
.
GetConstructor
(
new
[]
{
nullUnderlyingType
}));
}
}
if
(
item
.
Property
!=
null
)
if
(
specializedConstructor
==
null
)
{
il
.
Emit
(
OpCodes
.
Callvirt
,
item
.
Property
.
Setter
);
// stack is now [target]
}
else
{
{
il
.
Emit
(
OpCodes
.
Stfld
,
item
.
Field
);
// stack is now [target]
if
(
item
.
Property
!=
null
)
{
il
.
Emit
(
OpCodes
.
Callvirt
,
item
.
Property
.
Setter
);
// stack is now [target]
}
else
{
il
.
Emit
(
OpCodes
.
Stfld
,
item
.
Field
);
// stack is now [target]
}
}
}
il
.
Emit
(
OpCodes
.
Br_S
,
finishLabel
);
il
.
Emit
(
OpCodes
.
Br_S
,
finishLabel
);
...
@@ -1614,28 +1639,41 @@ static List<FieldInfo> GetSettableFields(Type t)
...
@@ -1614,28 +1639,41 @@ static List<FieldInfo> GetSettableFields(Type t)
il
.
Emit
(
OpCodes
.
Newobj
,
memberType
.
GetConstructor
(
new
[]
{
nullUnderlyingType
}));
il
.
Emit
(
OpCodes
.
Newobj
,
memberType
.
GetConstructor
(
new
[]
{
nullUnderlyingType
}));
}
}
}
}
if
(
item
.
Property
!
=
null
)
if
(
specializedConstructor
=
=
null
)
{
{
if
(
type
.
IsValueType
)
if
(
item
.
Property
!=
null
)
{
{
il
.
Emit
(
OpCodes
.
Call
,
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
else
{
{
il
.
Emit
(
OpCodes
.
Callvirt
,
item
.
Property
.
Setter
);
// stack is now [target]
il
.
Emit
(
OpCodes
.
Stfld
,
item
.
Field
);
// stack is now [target]
}
}
}
}
else
{
il
.
Emit
(
OpCodes
.
Stfld
,
item
.
Field
);
// stack is now [target]
}
il
.
Emit
(
OpCodes
.
Br_S
,
finishLabel
);
// stack is now [target]
il
.
Emit
(
OpCodes
.
Br_S
,
finishLabel
);
// stack is now [target]
il
.
MarkLabel
(
isDbNullLabel
);
// incoming stack: [target][target][value]
il
.
MarkLabel
(
isDbNullLabel
);
// incoming stack: [target][target][value]
if
(
specializedConstructor
!=
null
)
il
.
Emit
(
OpCodes
.
Pop
);
// stack is now [target][target]
{
il
.
Emit
(
OpCodes
.
Pop
);
// stack is now [target]
Type
itemType
=
item
.
Property
!=
null
?
item
.
Property
.
Type
:
item
.
Field
.
FieldType
;
if
(
itemType
.
IsValueType
)
il
.
Emit
(
OpCodes
.
Initobj
,
itemType
);
else
il
.
Emit
(
OpCodes
.
Ldnull
);
}
else
{
il
.
Emit
(
OpCodes
.
Pop
);
// stack is now [target][target]
il
.
Emit
(
OpCodes
.
Pop
);
// stack is now [target]
}
if
(
first
&&
returnNullIfFirstMissing
)
if
(
first
&&
returnNullIfFirstMissing
)
{
{
...
@@ -1656,6 +1694,10 @@ static List<FieldInfo> GetSettableFields(Type t)
...
@@ -1656,6 +1694,10 @@ static List<FieldInfo> GetSettableFields(Type t)
}
}
else
else
{
{
if
(
specializedConstructor
!=
null
)
{
il
.
Emit
(
OpCodes
.
Newobj
,
specializedConstructor
);
}
il
.
Emit
(
OpCodes
.
Stloc_1
);
// stack is empty
il
.
Emit
(
OpCodes
.
Stloc_1
);
// stack is empty
}
}
il
.
MarkLabel
(
allDone
);
il
.
MarkLabel
(
allDone
);
...
...
Tests/Tests.cs
浏览文件 @
b73acfce
...
@@ -33,29 +33,21 @@ public class ConcreteOrder : Order
...
@@ -33,29 +33,21 @@ public class ConcreteOrder : Order
}
}
}
}
class
NoDef
ua
ltConstructor
class
NoDef
au
ltConstructor
{
{
public
NoDef
ua
ltConstructor
(
int
a
)
public
NoDef
au
ltConstructor
(
int
a
)
{
{
A
=
a
;
A
=
a
;
}
}
public
int
A
{
get
;
set
;
}
public
int
A
{
get
;
set
;
}
}
}
public
void
EnsureNoConstructorGivesNiceErr
or
()
public
void
TestNoDefaultConstruct
or
()
{
{
try
NoDefaultConstructor
nodef
=
connection
.
Query
<
NoDefaultConstructor
>(
"select 1 A"
).
First
();
{
nodef
.
A
.
IsEqualTo
(
1
);
connection
.
Query
<
NoDefualtConstructor
>(
"select 1 A"
).
First
();
}
catch
(
InvalidOperationException
e
)
{
e
.
Message
.
IsEqualTo
(
"A parameterless default constructor is required to allow for dapper materialization"
);
}
}
}
// http://stackoverflow.com/q/8593871
// http://stackoverflow.com/q/8593871
public
void
TestAbstractInheritance
()
public
void
TestAbstractInheritance
()
{
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录