77<DemoBlock Title =" @Localizer[" NormalTitle " ]"
88 Introduction =" @Localizer[" NormalIntro " ]"
99 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
1512{
1613 [DataPropertyConverter (Type = typeof (byte []), Offset = 0 , Length = 5 )]
1714 public byte []? Header { get ; set ; }
@@ -25,12 +22,7 @@ class MockEntity
2522 [DataPropertyConverter (Type = typeof (int ), Offset = 8 , Length = 1 )]
2623 public int Value2 { get ; set ; }
2724} </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 >
3426 <ul class =" ul-demo" >
3527 <li ><b >Type</b >: 转换目标数据类型</li >
3628 <li ><b >Offset</b >: 数据偏移量,即在接收到的数据中起始位置</li >
@@ -64,8 +56,7 @@ class MockEntity
6456 <li ><code >DataUInt64LittleEndianConverter</code > 转成 ulong 无符号整形小端读取</li >
6557 </ul >
6658 <p >自定义数据类型转化器示例</p >
67- <Pre >[DataTypeConverter(Type = typeof(DataConverter< ; MockEntity> ; ))]
68- class MockEntity
59+ <Pre >class MockEntity
6960{
7061 [DataPropertyConverter (Type = typeof (byte []), Offset = 0 , Length = 5 )]
7162 public byte []? Header { get ; set ; }
@@ -109,20 +100,17 @@ if (attr is { Type: not null })
109100} </Pre >
110101 <Pre >service.ConfigureDataConverters(options =>
111102{
112- // 配置 MockEntity 转换
113- options .AddTypeConverter < ; MockEntity > ; ();
114-
115103 // 配置 MockEntity 属性 Header 转换逻辑 从起始位置 0 开始读取 5 个字节
116104 options .AddPropertyConverter < ; MockEntity > ; (entity => entity .Header , new DataPropertyConverterAttribute ()
117105 {
118106 Offset = 0 ,
119- Length = 5
107+ Length = 2
120108 });
121109
122110 // 配置 MockEntity 属性 Body 转换逻辑 从起始位置 5 开始读取 2 个字节
123111 options .AddPropertyConverter < ; MockEntity > ; (entity => entity .Body , new DataPropertyConverterAttribute ()
124112 {
125- Offset = 5 ,
113+ Offset = 3 ,
126114 Length = 2
127115 });
128116} );</Pre >
0 commit comments