解析具有固定长度字段的可变长度数组的消息
我需要解析(和构建)基于固定长度文本的消息,这些消息在某些情况下可能包含数组字段。
例子:
部分02SUBLOT1 SUBLOT2 03TEST1 结果1 测试2 结果2 测试3 结果3
如果这是一个对象,它可能会使用下面的 Lot 对象。
零件号 (PARTA)
批号 (LOTA)
2 个 SubLot 对象的数组(数量为 150 的 SUBLOT1 和数量为 999 的 SUBLOT2)
3 个测试结果的数组(TEST1,结果为 1234.67890,...)
请注意,消息中指定了数组项的数量。
我希望使用我见过人们谈论的 FileHelpers 库,但它似乎不支持多个数组字段,其中有另一个字段指定数量,并且它不支持本身具有该属性的字段类型[FixedLengthRecord()] 的。
这就是我希望能够做到的。请注意,字段长度 10 只是保持简单的一个产物。通常并非所有字段都定义为相同的长度。
[FixedLengthRecord()]
public class Lot
{
[FieldFixedLength(10)]
public string PartNumber { get; set; }
[FieldFixedLength(10)]
public string LotNumber { get; set; }
[FieldFixedLength(10)]
public SubLot[] SubLots { get; set; }
[FieldFixedLength(10)]
public Test[] Tests { get; set; }
}
[FixedLengthRecord()]
public class SubLot
{
[FieldFixedLength(10)]
public string SubLotNumber { get; set; }
[FieldFixedLength(10)]
public int Quantity { get; set; }
}
[FixedLengthRecord()]
public class Test
{
[FieldFixedLength(10)]
public string Description { get; set; }
[FieldFixedLength(10)]
public double Result { get; set; }
}
有人知道 FileHelpers 是否可以实现这一点吗?还有其他想法吗?我有许多不同的消息类型,因此我宁愿不为每种消息类型手动编码。 FileHelpers 中的属性装饰方法似乎是一个非常干净的解决方案,我正在考虑扩展它,但我想确保我不会错过更好的解决方案。
I have a need to parse (and build) fixed length text based messages that may in some cases contain array fields.
Example:
PARTA LOTA 02SUBLOT1 SUBLOT2 03TEST1 RESULT1 TEST2 RESULT2 TEST3 RESULT3
If this were an object, it might use the Lot object below.
Part Number (PARTA)
Lot Number (LOTA)
An Array of 2 SubLot Objects (SUBLOT1 with quantity 150 and SUBLOT2 with Quantity 999)
An Array of 3 Test Results (TEST1 with result 1234.67890, ...)
Note that the number of array items is specified in the message.
I was hoping to use the FileHelpers library that I've seen people talking about, but it doesn't appear to support multiple array fields where there is another field specifying the quantity, and it doesn't support field types that themselves have the attribute of [FixedLengthRecord()].
This is what I would like to be able to do. Note that the field length of 10 is just an artifact of keeping this simple. Not all fields would normally be defined with the same length.
[FixedLengthRecord()]
public class Lot
{
[FieldFixedLength(10)]
public string PartNumber { get; set; }
[FieldFixedLength(10)]
public string LotNumber { get; set; }
[FieldFixedLength(10)]
public SubLot[] SubLots { get; set; }
[FieldFixedLength(10)]
public Test[] Tests { get; set; }
}
[FixedLengthRecord()]
public class SubLot
{
[FieldFixedLength(10)]
public string SubLotNumber { get; set; }
[FieldFixedLength(10)]
public int Quantity { get; set; }
}
[FixedLengthRecord()]
public class Test
{
[FieldFixedLength(10)]
public string Description { get; set; }
[FieldFixedLength(10)]
public double Result { get; set; }
}
Anyone have any idea if this is possible with FileHelpers? Any other ideas? I have many different message types so I would rather not manually code for each one. The attribute decoration method in FileHelpers seems like a great clean solution and I'm considering just extending it, but I want to make sure I'm not missing a better solution out there.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我相信我过去也做过类似的事情。
我解决这个问题的方法是使用自定义属性。这使我能够创建类和嵌套对象,它们完全按照规范中的描述描述我的数据,并使用自定义属性来描述数据属性(长度、类型、填充要求,如果需要等)。
我还最终为类和属性编写了自定义序列化/反序列化,但这实际上是特定于实际应用程序的,因为数据是通过自定义政府协议传输的,该协议还通过加密套接字以固定大小的块或数据包发送和接收数据延续代码等。
教程
I believe that I done something very similar in the past.
The way that I tackled this issue is to use custom attributes. This allowed me to create classes and nested objects which described my data exactly as described in the specification and use custom attributes to describe the data attributes (lenght, type, padding requirements, if required etc).
I also ended up writing a custom serialization/deserialization for the classes and attributes however that was really specific to the actual application as the data was coming through a custom government protocol which also sent and received data in fixed sized chunks or packets over encrypted sockets with continuation codes etc.
Tutorials