Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
int
Dapper
提交
11c29b4a
D
Dapper
项目概览
int
/
Dapper
10 个月 前同步成功
通知
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,体验更适合开发者的 AI 搜索 >>
提交
11c29b4a
编写于
7月 14, 2014
作者:
M
Marc Gravell
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Check ITypeHandler.Parse actually gets chance to parse values
上级
353b682f
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
61 addition
and
2 deletion
+61
-2
Dapper NET40/SqlMapper.cs
Dapper NET40/SqlMapper.cs
+13
-2
Tests/Tests.cs
Tests/Tests.cs
+48
-0
未找到文件。
Dapper NET40/SqlMapper.cs
浏览文件 @
11c29b4a
...
...
@@ -1741,11 +1741,21 @@ private static CacheInfo GetCacheInfo(Identity identity, object exampleParameter
if
(!(
typeMap
.
ContainsKey
(
type
)
||
type
.
IsEnum
||
type
.
FullName
==
LinqBinary
||
(
type
.
IsValueType
&&
(
underlyingType
=
Nullable
.
GetUnderlyingType
(
type
))
!=
null
&&
underlyingType
.
IsEnum
)))
{
ITypeHandler
handler
;
if
(
typeHandlers
.
TryGetValue
(
type
,
out
handler
))
{
return
GetHandlerDeserializer
(
handler
,
type
,
startBound
);
}
return
GetTypeDeserializer
(
type
,
reader
,
startBound
,
length
,
returnNullIfFirstMissing
);
}
return
GetStructDeserializer
(
type
,
underlyingType
??
type
,
startBound
);
}
static
Func
<
IDataReader
,
object
>
GetHandlerDeserializer
(
ITypeHandler
handler
,
Type
type
,
int
startBound
)
{
return
(
IDataReader
reader
)
=>
handler
.
Parse
(
type
,
reader
.
GetValue
(
startBound
));
}
#if !CSHARP30
private
sealed
partial
class
DapperTable
...
...
@@ -3145,9 +3155,10 @@ public static void SetTypeMap(Type type, ITypeMap map)
{
Type
dataType
=
reader
.
GetFieldType
(
index
);
TypeCode
dataTypeCode
=
Type
.
GetTypeCode
(
dataType
),
unboxTypeCode
=
Type
.
GetTypeCode
(
unboxType
);
if
(
dataType
==
unboxType
||
dataTypeCode
==
unboxTypeCode
||
dataTypeCode
==
Type
.
GetTypeCode
(
nullUnderlyingType
))
bool
hasTypeHandler
;
if
((
hasTypeHandler
=
typeHandlers
.
ContainsKey
(
unboxType
))
||
dataType
==
unboxType
||
dataTypeCode
==
unboxTypeCode
||
dataTypeCode
==
Type
.
GetTypeCode
(
nullUnderlyingType
))
{
if
(
typeHandlers
.
ContainsKey
(
unboxType
)
)
if
(
hasTypeHandler
)
{
#pragma warning disable 618
il
.
EmitCall
(
OpCodes
.
Call
,
typeof
(
TypeHandlerCache
<>).
MakeGenericType
(
unboxType
).
GetMethod
(
"Parse"
),
null
);
// stack is now [target][target][typed-value]
...
...
Tests/Tests.cs
浏览文件 @
11c29b4a
...
...
@@ -3098,6 +3098,54 @@ class PracticeRebateOrders
public
string
TaxInvoiceNumber
{
get
{
return
fTaxInvoiceNumber
;
}
set
{
fTaxInvoiceNumber
=
value
;
}
}
}
public
class
RatingValueHandler
:
Dapper
.
SqlMapper
.
TypeHandler
<
RatingValue
>
{
private
RatingValueHandler
()
{
}
public
static
readonly
RatingValueHandler
Default
=
new
RatingValueHandler
();
public
override
RatingValue
Parse
(
object
value
)
{
if
(
value
is
Int32
)
return
new
RatingValue
()
{
Value
=
(
Int32
)
value
};
throw
new
FormatException
(
"Invalid conversion to RatingValue"
);
}
public
override
void
SetValue
(
System
.
Data
.
IDbDataParameter
parameter
,
RatingValue
value
)
{
// ... null, range checks etc ...
parameter
.
DbType
=
System
.
Data
.
DbType
.
Int32
;
parameter
.
Value
=
value
.
Value
;
}
}
public
class
RatingValue
{
public
Int32
Value
{
get
;
set
;
}
// ... some other properties etc ...
}
public
class
MyResult
{
public
String
CategoryName
{
get
;
set
;
}
public
RatingValue
CategoryRating
{
get
;
set
;
}
}
public
void
SO24740733_TestCustomValueHandler
()
{
Dapper
.
SqlMapper
.
AddTypeHandler
(
RatingValueHandler
.
Default
);
var
foo
=
connection
.
Query
<
MyResult
>(
"SELECT 'Foo' AS CategoryName, 200 AS CategoryRating"
).
Single
();
foo
.
CategoryName
.
IsEqualTo
(
"Foo"
);
foo
.
CategoryRating
.
Value
.
IsEqualTo
(
200
);
}
public
void
SO24740733_TestCustomValueSingleColumn
()
{
Dapper
.
SqlMapper
.
AddTypeHandler
(
RatingValueHandler
.
Default
);
var
foo
=
connection
.
Query
<
RatingValue
>(
"SELECT 200 AS CategoryRating"
).
Single
();
foo
.
Value
.
IsEqualTo
(
200
);
}
#if POSTGRESQL
class
Cat
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录