c# - Un-/Marshalling nested structures containing arrays of structures -
i want able receive binary info on tcp/ip consists of known structure. don't want inter-operate c or c++, solutions work case didn't help me. unfortunately other side cannot alter protocol. problem should arise when seek read binary file given format.
i checked binaryformatter
, similar utilize own format not acceptable me.
here sample set of structs. i'd able reconstruct nested arrays (of known length) of structs. current code exception:
could not load type 'nestedstruct' assembly '...' because contains object field @ offset 2 incorrectly aligned or overlapped non-object field.
i want able send/receive (or read/write) instances of struct mainstruct
.
[structlayout(layoutkind.explicit, pack = 1, size = 244, charset = charset.ansi)] public struct nestedstruct { [fieldoffset(0)] public int16 someint; [fieldoffset(2), marshalas(unmanagedtype.byvalarray, sizeconst = 242)] public byte[] characterarray; // array of fixed length 242 } [structlayout(layoutkind.explicit)] public struct othernestedstruct { [fieldoffset(0)] public int16 someint; [fieldoffset(2)] public int16 someotherint; } [structlayout(layoutkind.explicit)] public struct mainstruct { [fieldoffset(0)] public double somedouble; [fieldoffset(8)] public nestedstruct nestedcontent; [fieldoffset(8 + 244)] [marshalas(unmanagedtype.byvalarray, sizeconst = 13 * 4)] public othernestedstruct[] arrayofstruct; // fixed array length 13 }
update:
here latest version:
[structlayout(layoutkind.sequential, pack = 1)] public struct nestedstruct { public int16 someint; [marshalas(unmanagedtype.byvalarray, arraysubtype = unmanagedtype.u1, sizeconst = 242)] public byte[] characterarray; // array of fixed length 242 } [structlayout(layoutkind.sequential , pack=1)] public struct othernestedstruct { public int16 someint; public int16 someotherint; } [structlayout(layoutkind.sequential, pack=1)] public struct mainstruct { public double somedouble; public nestedstruct nestedcontent; [marshalas(unmanagedtype.byvalarray, arraysubtype = unmanagedtype.struct, sizeconst = 13)] public othernestedstruct[] arrayofstruct; // fixed array length 13 }
you must specify arraysubtype
[structlayout(layoutkind.sequential, pack = 1)] public struct nestedstruct { public int16 someint; [marshalas(unmanagedtype.byvalarray, arraysubtype = unmanagedtype.u1, sizeconst = 242)] public byte[] characterarray; // array of fixed length 242 } [structlayout(layoutkind.sequential, pack = 1)] public struct othernestedstruct { public int16 someint; public int16 someotherint; } [structlayout(layoutkind.sequential, pack = 1)] public struct mainstruct { public double somedouble; public nestedstruct nestedcontent; [marshalas(unmanagedtype.byvalarray, sizeconst = 13 * 4)] public othernestedstruct[] arrayofstruct; // fixed array length 13 } static void main(string[] args) { var x = marshal.sizeof(typeof(mainstruct)); //x == 460 }
c# binary marshalling binaryfiles unmarshalling
No comments:
Post a Comment