Skip to content

Commit accb812

Browse files
committed
doc: 更新类型检查文档
1 parent 6070a58 commit accb812

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/BootstrapBlazor.Server/Components/Samples/Sockets/DataEntities.razor

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,17 @@ class MockEntity
8282
{
8383
return new Foo() { Id = data.Span[0], Name = name };
8484
}
85+
}</Pre>
86+
<p>此处返回值会与参数 <code>Type = typeof(Foo)</code> 做数据合规检查,如果返回值无法转换为指定 <code>Type</code> 时不进行赋值操作,代码逻辑如下</p>
87+
<Pre>var attr = p.GetCustomAttribute&lt;DataPropertyConverterAttribute&gt;(false) ?? GetPropertyConverterAttribute(p);
88+
if (attr is { Type: not null })
89+
{
90+
var value = attr.ConvertTo(data);
91+
var valueType = value?.GetType();
92+
if (valueType != null && p.PropertyType.IsAssignableFrom(valueType))
93+
{
94+
p.SetValue(entity, value);
95+
}
8596
}</Pre>
8697
<p>更多技术细节可以参考以上内置提供的转换器源码</p>
8798
</DemoBlock>

0 commit comments

Comments
 (0)