cla*_*b86 5 .net c# reflection binary serialization
场景:我通过 HTTP 接收到原始二进制数据并将数据存储到一个字节数组中。我有描述二进制数据可以表示的各种字段的文档,但必须在运行时确定数据的实际含义。例如,如果表示发生错误的字节 = 1,则下一个字节的含义发生变化。
在 .NET 4 中使用 C#,我想创建一个或多个反映文档中描述的字段的类,然后使用二进制数据的字节数组以某种方式初始化这些类。我希望该解决方案能够最大限度地减少代码重复,并且要模块化和优雅。
我已经研究过创建 Serializable 类,但我不知道它是如何工作的,因为我从一个不是由我创建(因此没有序列化)的字节数组开始。
我还尝试使用泛型和反射来检索自定义类中包含的字段的大小和类型。然后我使用该信息从字节数组中动态切出数据并将其分配给相应的字段。然而,这种方法导致了很多丑陋、难以管理的代码。
任何有关为此问题设计可扩展的解耦解决方案的建议或指示将不胜感激。
编辑:包含反映规范中字段的字段的类示例
public class PriceHistoryResponse : BinaryResponse
{
public List<Quote> quotes { get; set; }
private CountData countData { get; set; }
private EndingDelimiterSection endingDelimiterSection { get; set; }
/* This code performs the logic needed to check for optional fields
and to find the number of times that certain fields are repeated */
public PriceHistoryResponse(byte[] responseBytes) : base(responseBytes)
{
countData = new CountData();
ParseResponseSection(countData);
quotes = new List<Quote>();
for (int i = 0; i < countData.quoteCount; i++)
{
quotes.Add(new Quote());
quotes[i].symbolData = new SymbolData();
ParseResponseSection(quotes[i].symbolData);
if (quotes[i].symbolData.errorCode == 1)
{
quotes[i].errorData = new ErrorData();
ParseResponseSection(quotes[i].errorData);
break;
}
quotes[i].chartBarData = new ChartBarData();
ParseResponseSection(quotes[i].chartBarData);
quotes[i].chartBars = new List<ChartBar>();
for (int j = 0; j < quotes[i].chartBarData.chartBarCount; j++)
{
quotes[i].chartBars.Add(new ChartBar());
ParseResponseSection(quotes[i].chartBars[j]);
}
}
endingDelimiterSection = new EndingDelimiterSection();
ParseResponseSection(endingDelimiterSection);
}
}
class CountData : IResponseSection
{
public int quoteCount { get; set; }
}
public class Quote
{
public SymbolData symbolData { get; set; }
public ErrorData errorData { get; set; }
public ChartBarData chartBarData { get; set; }
public List<ChartBar> chartBars { get; set; }
}
public class SymbolData : IResponseSection
{
public string symbol { get; set; }
public byte errorCode { get; set; }
}
public class ErrorData : IResponseSection
{
public string errorText { get; set; }
}
public class ChartBarData : IResponseSection
{
public int chartBarCount { get; set; }
}
public class ChartBar : IResponseSection
{
public float close { get; set; }
public float high { get; set; }
public float low { get; set; }
public float open { get; set; }
public float volume { get; set; }
public long timestamp { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我将你的代码粘贴到 VS 中,单击“生成方法存根”几次并将其移动一些。我想这会成功。
您提供的代码非常聪明,它有点像访问者模式,其中重载切换到正确的方法。
public class BinaryResponse {
private BinaryReader _rdr;
public BinaryResponse(byte[] responseBytes) {
_rdr = new BinaryReader(new MemoryStream(responseBytes)); // wrap the byte[] in a BinaryReader to be able to pop the bytes off the top
}
protected void ParseResponseSection(CountData countData) {
countData.quoteCount = _rdr.ReadInt16(); // guessing 64.000 quotes should be enough in one response, the documentation will have the type
}
protected void ParseResponseSection(SymbolData symbolData) {
symbolData.errorCode = _rdr.ReadByte(); // depending on your format, where is the ErrorCOde in the byte[]? the symbol might be first
int symbolLength = _rdr.ReadInt16(); // if it's not written by a .Net WriteString on the other end better to read this count yourelf
symbolData.symbol = new string(_rdr.ReadChars(symbolLength)); // read the chars and put into string
}
protected void ParseResponseSection(ErrorData errorData) {
int errorLenth = _rdr.ReadInt16();
errorData.errorText = new string(_rdr.ReadChars(errorLenth));
}
protected void ParseResponseSection(ChartBarData chartBarData) {
chartBarData.chartBarCount = _rdr.ReadInt16();
}
protected void ParseResponseSection(ChartBar chartBar) {
// check the order with the documentation, also maybe some casting is needed because other types are in the byte[]
chartBar.close = _rdr.ReadSingle();
chartBar.high = _rdr.ReadSingle();
chartBar.low = _rdr.ReadSingle();
chartBar.open = _rdr.ReadSingle();
chartBar.timestamp = _rdr.ReadInt64();
}
protected void ParseResponseSection(EndingDelimiterSection endingDelimiterSection) {
int checkValue = _rdr.ReadInt16();
if (checkValue != 12345) throw new InvalidDataException("Corrupt Response! Expecting End Delimiter"); // assert that the end delimiter is some value
}
}
Run Code Online (Sandbox Code Playgroud)
这是您要找的吗?您没有提及任何有关编码的内容,在读取字节等时可能需要考虑到这一点。
问候格特-扬
归档时间: |
|
查看次数: |
3366 次 |
最近记录: |