Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
dotNET Platform
runtime
提交
9fedcc55
R
runtime
项目概览
dotNET Platform
/
runtime
10 个月 前同步成功
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
runtime
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
前往新版Gitcode,体验更适合开发者的 AI 搜索 >>
未验证
提交
9fedcc55
编写于
7月 17, 2023
作者:
L
Layomi Akinrinade
提交者:
GitHub
7月 17, 2023
1
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Fix binding logic for dictionaries with complex elements (#89045)
上级
8e9222b9
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
122 addition
and
1 deletion
+122
-1
src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/CoreBindingHelper.cs
...iguration.Binder/gen/Helpers/Emitter/CoreBindingHelper.cs
+1
-1
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs
...nder/tests/Common/ConfigurationBinderTests.TestClasses.cs
+56
-0
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
...iguration.Binder/tests/Common/ConfigurationBinderTests.cs
+65
-0
未找到文件。
src/libraries/Microsoft.Extensions.Configuration.Binder/gen/Helpers/Emitter/CoreBindingHelper.cs
浏览文件 @
9fedcc55
...
...
@@ -731,7 +731,7 @@ void Emit_BindAndAddLogic_ForElement(string parsedKeyExpr)
""");
}
EmitBindCoreCall
(
elementType
,
$"
{
Identifier
.
element
}
!
"
,
Identifier
.
section
,
InitializationKind
.
None
);
EmitBindCoreCall
(
elementType
,
$"
{
Identifier
.
element
}
"
,
Identifier
.
section
,
InitializationKind
.
None
);
_writer
.
WriteLine
(
$"
{
objIdentifier
}
[
{
parsedKeyExpr
}
] =
{
Identifier
.
element
}
;"
);
}
...
...
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.TestClasses.cs
浏览文件 @
9fedcc55
...
...
@@ -3,6 +3,8 @@
using
System
;
using
System.Collections.Generic
;
using
System.ComponentModel
;
using
System.Diagnostics.CodeAnalysis
;
using
System.Globalization
;
using
System.Linq
;
using
Microsoft.Extensions.Configuration
;
...
...
@@ -664,5 +666,59 @@ public struct StructWithParameterlessAndParameterizedCtor
public
int
MyInt
{
get
;
}
}
[
TypeConverter
(
typeof
(
GeolocationTypeConverter
))]
public
struct
Geolocation
:
IEquatable
<
Geolocation
>,
IParsable
<
Geolocation
>
{
public
static
readonly
Geolocation
Zero
=
new
(
0
,
0
);
public
Geolocation
(
double
latitude
,
double
longitude
)
{
Latitude
=
latitude
;
Longitude
=
longitude
;
}
public
double
Latitude
{
get
;
set
;
}
public
double
Longitude
{
get
;
set
;
}
private
sealed
class
GeolocationTypeConverter
:
TypeConverter
{
public
override
bool
CanConvertFrom
(
ITypeDescriptorContext
?
context
,
Type
sourceType
)
{
if
(
sourceType
==
typeof
(
string
)
||
sourceType
==
typeof
(
Geolocation
))
{
return
true
;
}
return
base
.
CanConvertFrom
(
context
,
sourceType
);
}
public
override
object
?
ConvertFrom
(
ITypeDescriptorContext
?
context
,
CultureInfo
?
culture
,
object
value
)
{
if
(
value
is
string
s
)
{
return
Parse
(
s
,
culture
);
}
else
if
(
value
is
Geolocation
geolocation
)
{
return
geolocation
;
}
return
base
.
ConvertFrom
(
context
,
culture
,
value
);
}
}
public
bool
Equals
(
Geolocation
other
)
=>
Latitude
==
other
.
Latitude
&&
Longitude
==
other
.
Longitude
;
public
static
Geolocation
Parse
(
string
s
,
IFormatProvider
?
provider
)
=>
throw
new
NotImplementedException
();
public
static
bool
TryParse
([
NotNullWhen
(
true
)]
string
?
s
,
IFormatProvider
?
provider
,
[
MaybeNullWhen
(
false
)]
out
Geolocation
result
)
=>
throw
new
NotImplementedException
();
}
public
class
GeolocationWrapper
{
public
Geolocation
Location
{
get
;
set
;
}
}
}
}
src/libraries/Microsoft.Extensions.Configuration.Binder/tests/Common/ConfigurationBinderTests.cs
浏览文件 @
9fedcc55
...
...
@@ -1900,5 +1900,70 @@ public void AllowsCaseInsensitiveMatch()
GenericOptionsWithParamCtor
<
string
>
obj2
=
configuration
.
Get
<
GenericOptionsWithParamCtor
<
string
>>();
Assert
.
Equal
(
"MyString"
,
obj2
.
Value
);
}
[
Fact
]
public
void
ObjWith_IParsableT_And_TypeConverter
()
{
var
configuration
=
TestHelpers
.
GetConfigurationFromJsonString
(
"""
{
"Location"
:
{
"Latitude"
:
3
,
"Longitude"
:
4
,
}
}
""");
// Neither IParsableT or TypeConverter impl are honored (https://github.com/dotnet/runtime/issues/83599).
GeolocationWrapper
obj
=
configuration
.
Get
<
GeolocationWrapper
>();
ValidateGeolocation
(
obj
.
Location
);
configuration
=
TestHelpers
.
GetConfigurationFromJsonString
(
""" { "
Geolocation
": "
3
,
4
", } """
);
obj
=
configuration
.
Get
<
GeolocationWrapper
>();
Assert
.
Equal
(
Geolocation
.
Zero
,
obj
.
Location
);
}
[
Fact
]
public
void
ComplexObj_As_Dictionary_Element
()
{
var
configuration
=
TestHelpers
.
GetConfigurationFromJsonString
(
"""
{
"First"
:
{
"Latitude"
:
3
,
"Longitude"
:
4
,
}
}
""");
Geolocation
obj
=
configuration
.
Get
<
IDictionary
<
string
,
Geolocation
>>()[
"First"
];
ValidateGeolocation
(
obj
);
obj
=
configuration
.
Get
<
IReadOnlyDictionary
<
string
,
Geolocation
>>()[
"First"
];
ValidateGeolocation
(
obj
);
}
[
Fact
]
public
void
ComplexObj_As_Enumerable_Element
()
{
var
configuration
=
TestHelpers
.
GetConfigurationFromJsonString
(
"""{ "
Enumerable
": [{ "
Latitude
": 3, "
Longitude
": 4 }] }"""
)
.
GetSection
(
"Enumerable"
);
Geolocation
obj
=
configuration
.
Get
<
IList
<
Geolocation
>>()[
0
];
ValidateGeolocation
(
obj
);
obj
=
configuration
.
Get
<
Geolocation
[
]>
()[
0
];
ValidateGeolocation
(
obj
);
obj
=
configuration
.
Get
<
IReadOnlyList
<
Geolocation
>>()[
0
];
ValidateGeolocation
(
obj
);
}
private
void
ValidateGeolocation
(
Geolocation
location
)
{
Assert
.
Equal
(
3
,
location
.
Latitude
);
Assert
.
Equal
(
4
,
location
.
Longitude
);
}
}
}
麦壳饼
@mysticboy
mentioned in commit
3aeaff95
·
7月 31, 2023
mentioned in commit
3aeaff95
mentioned in commit 3aeaff95b4a81233ec368bcdb9dfc1dcd4edee4c
开关提交列表
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录