一个结构中的C#数组 [英] C# array within a struct
问题描述
想这样做:
(编辑:坏的样本code,忽略以下略)
结构RECORD {
的char [] NAME =新的char [16];
INT DT1;
}
结构BLOCK {
的char [] =版本新的char [4];
诠释FIELD1;
诠释域2;
录像[] =记录新纪录[15];
的char [] = filler1新的char [24];
}
但暂时无法申报结构数组大小,我该如何重新配置呢?
编辑:究其原因,布局是我使用BinaryReader读取用C编写结构的文件。使用BinaryReader和C#结构联盟(FieldOffset(0)),我想加载头为一个字节数组,然后读取它,因为它最初的目的。
[StructLayout(LayoutKind.Sequential)]
不安全的结构headerLayout
{
[FieldOffset(0)]
的char [] =版本新的char [4];
INT fileOsn;
INT FILEDSN;
//等领域,一些简单类型的数组
}[StructLayout(LayoutKind.Explicit)
在头结构headerUnion // 2048字节
{
[FieldOffset(0)]
公众的byte [] headerBytes; //为BinaryReader
[FieldOffset(0)]
公共headerLayout头; //现场确认
}
我也不会首先使用该模式。这种内存映射可能是在C为宜,而不是像C#高级语言。
我刚刚写为我想读的每个成员二进制读者的电话。这意味着你可以使用类和他们在一个干净的高水平方式编写。
这也需要照顾的尾数问题。在不同的端系统中使用时,虽然内存映射将打破。
相关问题:<一href=\"http://stackoverflow.com/questions/6335153/casting-a-byte-array-to-a-managed-structure/6336196#6336196\">Casting一个字节数组到托管结构
所以你的code将类似于以下(添加访问修饰符等):
类记录
{
的char []的名称;
INT DT1;
}
类块{
的char []版本;
诠释FIELD1;
诠释域2;
录像[]记录;
的char [] filler1;
}类MyReader
{
BinaryReader读卡器; 块读出数据块()
{
一块块=新的块();
block.version = Reader.ReadChars(4);
block.field1 = Reader.ReadInt32();
block.field2 = Reader.ReadInt32();
block.records =新的记录[15];
的for(int i = 0; I&LT; block.records.Length;我++)
block.records [I] = ReadRecord();
block.filler1 = Reader.ReadChars(24);
返回块;
} 记录ReadRecord()
{
...
} 公共MyReader(BinaryReader读卡器)
{
读卡器=阅读器;
}
}
Want to do this: (EDIT: bad sample code, ignore and skip below)
struct RECORD {
char[] name = new char[16];
int dt1;
}
struct BLOCK {
char[] version = new char[4];
int field1;
int field2;
RECORD[] records = new RECORD[15];
char[] filler1 = new char[24];
}
But being unable to declare array sizes in struct, how do I reconfigure this?
EDIT: The reason for the layout is I'm using BinaryReader to read a file written with C structs. Using BinaryReader, and a C# struct union (FieldOffset(0)), I'm wanting to load the header as a byte array, then read it as it was intended originally.
[StructLayout(LayoutKind.Sequential)]
unsafe struct headerLayout
{
[FieldOffset(0)]
char[] version = new char[4];
int fileOsn;
int fileDsn;
// and other fields, some with arrays of simple types
}
[StructLayout(LayoutKind.Explicit)]
struct headerUnion // 2048 bytes in header
{
[FieldOffset(0)]
public byte[] headerBytes; // for BinaryReader
[FieldOffset(0)]
public headerLayout header; // for field recognition
}
I wouldn't use that pattern in the first place. This kind of memory mapping may be appropriate in c, but not in a high level language like C#.
I'd just write a call to the binary reader for each member I want to read. This means you can use classes and write them in a clean high level way.
It also takes care of endian issues. Whereas memory mapping will break when used on different endian systems.
Related question: Casting a byte array to a managed structure
So your code would look similar to the following (add access modifiers etc.):
class Record
{
char[] name;
int dt1;
}
class Block {
char[] version;
int field1;
int field2;
RECORD[] records;
char[] filler1;
}
class MyReader
{
BinaryReader Reader;
Block ReadBlock()
{
Block block=new Block();
block.version=Reader.ReadChars(4);
block.field1=Reader.ReadInt32();
block.field2=Reader.ReadInt32();
block.records=new Record[15];
for(int i=0;i<block.records.Length;i++)
block.records[i]=ReadRecord();
block.filler1=Reader.ReadChars(24);
return block;
}
Record ReadRecord()
{
...
}
public MyReader(BinaryReader reader)
{
Reader=reader;
}
}
这篇关于一个结构中的C#数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!