Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
236a948d
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,发现更多精彩内容 >>
提交
236a948d
编写于
6月 01, 2011
作者:
M
mgravell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
parse enums from string, ala L2S
上级
1f7d2be3
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
71 addition
and
7 deletion
+71
-7
Dapper/SqlMapper.cs
Dapper/SqlMapper.cs
+51
-5
Tests/Tests.cs
Tests/Tests.cs
+20
-2
未找到文件。
Dapper/SqlMapper.cs
浏览文件 @
236a948d
...
...
@@ -849,6 +849,11 @@ private static int ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction,
return
(
T
)
val
;
};
}
static
readonly
MethodInfo
enumParse
=
typeof
(
Enum
).
GetMethod
(
"Parse"
,
new
Type
[]
{
typeof
(
Type
),
typeof
(
string
),
typeof
(
bool
)
}),
getItem
=
typeof
(
IDataRecord
).
GetProperties
(
BindingFlags
.
Instance
|
BindingFlags
.
Public
)
.
Where
(
p
=>
p
.
GetIndexParameters
().
Any
()
&&
p
.
GetIndexParameters
()[
0
].
ParameterType
==
typeof
(
int
))
.
Select
(
p
=>
p
.
GetGetMethod
()).
First
();
public
static
Func
<
IDataReader
,
T
>
GetClassDeserializer
<
T
>(
#if CSHARP30
...
...
@@ -863,6 +868,7 @@ private static int ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction,
var
il
=
dm
.
GetILGenerator
();
il
.
DeclareLocal
(
typeof
(
int
));
il
.
DeclareLocal
(
typeof
(
T
));
bool
haveEnumLocal
=
false
;
il
.
Emit
(
OpCodes
.
Ldc_I4_0
);
il
.
Emit
(
OpCodes
.
Stloc_0
);
var
properties
=
typeof
(
T
)
...
...
@@ -896,11 +902,6 @@ private static int ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction,
select
new
{
Name
=
n
,
Property
=
prop
,
Field
=
field
}
).
ToList
();
var
getItem
=
typeof
(
IDataRecord
).
GetProperties
(
BindingFlags
.
Instance
|
BindingFlags
.
Public
)
.
Where
(
p
=>
p
.
GetIndexParameters
().
Any
()
&&
p
.
GetIndexParameters
()[
0
].
ParameterType
==
typeof
(
int
))
.
Select
(
p
=>
p
.
GetGetMethod
()).
First
();
int
index
=
startBound
;
il
.
BeginExceptionBlock
();
...
...
@@ -922,6 +923,7 @@ private static int ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction,
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]
il
.
Emit
(
OpCodes
.
Dup
);
// stack is now [target][target][value][value]
il
.
Emit
(
OpCodes
.
Isinst
,
typeof
(
DBNull
));
// stack is now [target][target][value-as-object][DBNull or null]
il
.
Emit
(
OpCodes
.
Brtrue_S
,
isDbNullLabel
);
// stack is now [target][target][value-as-object]
...
...
@@ -930,6 +932,50 @@ private static int ExecuteCommand(IDbConnection cnn, IDbTransaction tranaction,
Type
memberType
=
item
.
Property
!=
null
?
item
.
Property
.
Type
:
item
.
Field
.
FieldType
;
var
nullUnderlyingType
=
Nullable
.
GetUnderlyingType
(
memberType
);
var
unboxType
=
nullUnderlyingType
!=
null
&&
nullUnderlyingType
.
IsEnum
?
nullUnderlyingType
:
memberType
;
if
(
unboxType
.
IsEnum
)
{
if
(!
haveEnumLocal
)
{
il
.
DeclareLocal
(
typeof
(
string
));
haveEnumLocal
=
true
;
}
Label
isNotString
=
il
.
DefineLabel
();
il
.
Emit
(
OpCodes
.
Dup
);
// stack is now [target][target][value][value]
il
.
Emit
(
OpCodes
.
Isinst
,
typeof
(
string
));
// stack is now [target][target][value-as-object][string or null]
il
.
Emit
(
OpCodes
.
Dup
);
// stack is now [target][target][value-as-object][string or null][string or null]
il
.
Emit
(
OpCodes
.
Stloc_2
);
// stack is now [target][target][value-as-object][string or null]
il
.
Emit
(
OpCodes
.
Brfalse_S
,
isNotString
);
// stack is now [target][target][value-as-object]
il
.
Emit
(
OpCodes
.
Pop
);
// stack is now [target][target]
il
.
Emit
(
OpCodes
.
Ldtoken
,
unboxType
);
// stack is now [target][target][enum-type-token]
il
.
EmitCall
(
OpCodes
.
Call
,
typeof
(
Type
).
GetMethod
(
"GetTypeFromHandle"
),
null
);
// stack is now [target][target][enum-type]
il
.
Emit
(
OpCodes
.
Ldloc_2
);
// stack is now [target][target][enum-type][string]
il
.
Emit
(
OpCodes
.
Ldc_I4_1
);
// stack is now [target][target][enum-type][string][true]
il
.
EmitCall
(
OpCodes
.
Call
,
enumParse
,
null
);
// stack is now [target][target][enum-as-object]
il
.
Emit
(
OpCodes
.
Unbox_Any
,
unboxType
);
// stack is now [target][target][typed-value]
if
(
nullUnderlyingType
!=
null
)
{
il
.
Emit
(
OpCodes
.
Newobj
,
memberType
.
GetConstructor
(
new
[]
{
nullUnderlyingType
}));
}
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
.
MarkLabel
(
isNotString
);
}
il
.
Emit
(
OpCodes
.
Unbox_Any
,
unboxType
);
// stack is now [target][target][typed-value]
if
(
nullUnderlyingType
!=
null
&&
nullUnderlyingType
.
IsEnum
)
{
...
...
Tests/Tests.cs
浏览文件 @
236a948d
...
...
@@ -528,10 +528,28 @@ class TestEnumClass
{
public
TestEnum
?
EnumEnum
{
get
;
set
;
}
}
class
TestEnumClassNoNull
{
public
TestEnum
EnumEnum
{
get
;
set
;
}
}
public
void
TestEnumWeirdness
()
{
connection
.
Query
<
TestEnumClass
>(
"select null as [EnumEnum]"
);
connection
.
Query
<
TestEnumClass
>(
"select cast(1 as tinyint) as [EnumEnum]"
);
connection
.
Query
<
TestEnumClass
>(
"select null as [EnumEnum]"
).
First
().
EnumEnum
.
IsEqualTo
(
null
);
connection
.
Query
<
TestEnumClass
>(
"select cast(1 as tinyint) as [EnumEnum]"
).
First
().
EnumEnum
.
IsEqualTo
(
TestEnum
.
Bla
);
}
void
Foo
()
{
string
s
=
"Bla"
;
var
obj
=
new
TestEnumClassNoNull
();
obj
.
EnumEnum
=
(
TestEnum
)
Enum
.
Parse
(
typeof
(
TestEnum
),
s
,
true
);
}
public
void
TestEnumStrings
()
{
connection
.
Query
<
TestEnumClassNoNull
>(
"select 'BLA' as [EnumEnum]"
).
First
().
EnumEnum
.
IsEqualTo
(
TestEnum
.
Bla
);
connection
.
Query
<
TestEnumClassNoNull
>(
"select 'bla' as [EnumEnum]"
).
First
().
EnumEnum
.
IsEqualTo
(
TestEnum
.
Bla
);
connection
.
Query
<
TestEnumClass
>(
"select 'BLA' as [EnumEnum]"
).
First
().
EnumEnum
.
IsEqualTo
(
TestEnum
.
Bla
);
connection
.
Query
<
TestEnumClass
>(
"select 'bla' as [EnumEnum]"
).
First
().
EnumEnum
.
IsEqualTo
(
TestEnum
.
Bla
);
}
public
void
TestSupportForParamDictionary
()
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录