.net - C# StructLayout.Explicit Question -


I'm trying to understand that there is no problem in the second example below, but the first example gives me the following exception I think that both examples should be given an exception based on the details. Can anyone illuminate me? Unchecked exception: System.TypeLoadException: 'StructTest.OuterType' type assembly 'Strettext, version = 1.0.0.0, culture = neutral, PublicKeyToken = null' because in it there is no object field on offset 0 Which is incorrectly overlap with the alliance or non-object field. Press any key to continue on StreakTest.program.mine (string [] Args). . .

Example 1

  using the system; Using System.Collections.Generic; Using System.Linq; Using System.Runtime.InteropServices; Using System.Text; Namespace Structured Test {[Structure layout (Sequik, Pack = 1)] Structure Entertype {[Marshall AS (uncountable type Bevelar, size = 100)] four [] buffer; } [Layout Layout (LayoutCind.explicet)] Straight Outtertype {[Field Offset (0); Some Value; [Field Offset (0)] Internship Some other values; } Class Program {Static Wide Men (String [] ARGs) {OutType T = New OpType (); System.Console.WriteLine (t); }}}  

Example 2

  using the system; Using System.Collections.Generic; Using System.Linq; Using System.Runtime.InteropServices; Using System.Text; Namespace Structured Test {[Structure layout (Sequik, Pack = 1)] Structure Entertype {[Marshall AS (uncountable type Bevelar, size = 100)] four [] buffer; } [Structure layout (LayoutCind.explicet)] Straight outertype {[Field offset (4)] Some values ​​of private int. [Field Offset (0)] Internship Some other values; } Class Program {Static Wide Men (String [] ARGs) {OutType T = New OpType (); System.Console.WriteLine (t); There is a validator in the common language runtime, which ensures that the current code (code) verified IL can not possibly have corrupted memory in the managed environment, it is saved from declaring you a structure in which the fields overlap. . Basically, your data consists of two data members, an integer (which is 4 bytes) and a native integer (indicator size). On a 32 bit CLR, in which you are probably running your code,  char []  will take 4 bytes so that if you reduce the integer less than four bytes from the beginning of the structure, the overlapping field will be It is interesting to note that both your code snippets fail on the 64 bit runtime because the indicator size is 8 bytes. 


Comments

Popular posts from this blog

c# - ListView onScroll event -

PHP - get image from byte array -

Linux Terminal Problem with Non-Canonical Terminal I/O app -