7
7
<DemoBlock Title =" @Localizer[" NormalTitle " ]"
8
8
Introduction =" @Localizer[" NormalIntro " ]"
9
9
Name =" Normal" ShowCode =" false" >
10
- <p >
11
- 通过 <code >DataTypeConverterAttribute</code > 类标签与 <code >DataPropertyConverterAttribute</code >
12
- 属性标签可以将通讯数据自动转化为我们系统中需要的业务实体类,示例如下:</p >
13
- <Pre >[DataTypeConverter(Type = typeof(DataConverter< ; MockEntity> ; ))]
14
- class MockEntity
10
+ <p >通过 <code >DataPropertyConverterAttribute</code > 属性标签可以将通讯数据自动转化为我们系统中需要的业务实体类,示例如下:</p >
11
+ <Pre >class MockEntity
15
12
{
16
13
[DataPropertyConverter (Type = typeof (byte []), Offset = 0 , Length = 5 )]
17
14
public byte []? Header { get ; set ; }
@@ -25,12 +22,7 @@ class MockEntity
25
22
[DataPropertyConverter (Type = typeof (int ), Offset = 8 , Length = 1 )]
26
23
public int Value2 { get ; set ; }
27
24
} </Pre >
28
- <p class =" code-label" >1. <code >DataTypeConverter</code > 参数说明:</p >
29
- <ul class =" ul-demo" >
30
- <li ><b >Type</b >: 自定义转换器类型,组件库内置了 <code >DataConverter</code > 泛型类,建议看一下源码非常方便扩展出自己的转换器
31
- </li >
32
- </ul >
33
- <p class =" code-label" >2. <code >DataPropertyConverterAttribute</code > 参数说明</p >
25
+ <p class =" code-label" >1. <code >DataPropertyConverterAttribute</code > 参数说明</p >
34
26
<ul class =" ul-demo" >
35
27
<li ><b >Type</b >: 转换目标数据类型</li >
36
28
<li ><b >Offset</b >: 数据偏移量,即在接收到的数据中起始位置</li >
@@ -64,8 +56,7 @@ class MockEntity
64
56
<li ><code >DataUInt64LittleEndianConverter</code > 转成 ulong 无符号整形小端读取</li >
65
57
</ul >
66
58
<p >自定义数据类型转化器示例</p >
67
- <Pre >[DataTypeConverter(Type = typeof(DataConverter< ; MockEntity> ; ))]
68
- class MockEntity
59
+ <Pre >class MockEntity
69
60
{
70
61
[DataPropertyConverter (Type = typeof (byte []), Offset = 0 , Length = 5 )]
71
62
public byte []? Header { get ; set ; }
@@ -109,20 +100,17 @@ if (attr is { Type: not null })
109
100
} </Pre >
110
101
<Pre >service.ConfigureDataConverters(options =>
111
102
{
112
- // 配置 MockEntity 转换
113
- options .AddTypeConverter < ; MockEntity > ; ();
114
-
115
103
// 配置 MockEntity 属性 Header 转换逻辑 从起始位置 0 开始读取 5 个字节
116
104
options .AddPropertyConverter < ; MockEntity > ; (entity => entity .Header , new DataPropertyConverterAttribute ()
117
105
{
118
106
Offset = 0 ,
119
- Length = 5
107
+ Length = 2
120
108
});
121
109
122
110
// 配置 MockEntity 属性 Body 转换逻辑 从起始位置 5 开始读取 2 个字节
123
111
options .AddPropertyConverter < ; MockEntity > ; (entity => entity .Body , new DataPropertyConverterAttribute ()
124
112
{
125
- Offset = 5 ,
113
+ Offset = 3 ,
126
114
Length = 2
127
115
});
128
116
} );</Pre >
0 commit comments