C# StructLayout.Explicit 问题
我试图理解为什么下面的第二个示例没有问题,但第一个示例给了我下面的例外。 在我看来,这两个例子都应该根据描述给出例外。 谁能启发我吗?
未处理的异常: System.TypeLoadException:无法 从加载类型“StructTest.OuterType” 程序集'StructTest,版本=1.0.0.0, 文化=中立,PublicKeyToken=null' 因为它包含一个对象字段 偏移量 0 未正确对齐 或与非对象字段重叠。
在 StructTest.Program.Main(String[] args) 按任意键继续。 。 .
示例 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace StructTest
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct InnerType
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
char[] buffer;
}
[StructLayout(LayoutKind.Explicit)]
struct OuterType
{
[FieldOffset(0)]
int someValue;
[FieldOffset(0)]
InnerType someOtherValue;
}
class Program
{
static void Main(string[] args)
{
OuterType t = new OuterType();
System.Console.WriteLine(t);
}
}
}
示例 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace StructTest
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct InnerType
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
char[] buffer;
}
[StructLayout(LayoutKind.Explicit)]
struct OuterType
{
[FieldOffset(4)]
private int someValue;
[FieldOffset(0)]
InnerType someOtherValue;
}
class Program
{
static void Main(string[] args)
{
OuterType t = new OuterType();
System.Console.WriteLine(t);
}
}
}
I'm trying to understand why the second example below works with no issues, but the first example gives me the exception below. It seems to me that both examples should give an exception based on the description. Can anyone enlighten me?
Unhandled Exception:
System.TypeLoadException: Could not
load type 'StructTest.OuterType' from
assembly 'StructTest, Version=1.0.0.0,
Culture=neutral, PublicKeyToken=null'
because it contains an object field at
offset 0 that is incorrectly aligned
or overlapped by a non-object field.
at StructTest.Program.Main(String[]
args) Press any key to continue . . .
Example 1
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace StructTest
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct InnerType
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
char[] buffer;
}
[StructLayout(LayoutKind.Explicit)]
struct OuterType
{
[FieldOffset(0)]
int someValue;
[FieldOffset(0)]
InnerType someOtherValue;
}
class Program
{
static void Main(string[] args)
{
OuterType t = new OuterType();
System.Console.WriteLine(t);
}
}
}
Example 2
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
namespace StructTest
{
[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct InnerType
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)]
char[] buffer;
}
[StructLayout(LayoutKind.Explicit)]
struct OuterType
{
[FieldOffset(4)]
private int someValue;
[FieldOffset(0)]
InnerType someOtherValue;
}
class Program
{
static void Main(string[] args)
{
OuterType t = new OuterType();
System.Console.WriteLine(t);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
公共语言运行时包含一个验证器,可确保运行的代码(可验证的 IL)不可能损坏托管环境中的内存。 这可以防止您声明字段重叠的结构。 基本上,您的结构包含两个数据成员。 一个整数(4 个字节)和一个本机整数(指针大小)。 在 32 位 CLR 上(您可能正在其中运行代码),
char[]
将占用 4 个字节,因此,如果您将整数放在距结构开头不到 4 个字节的位置,则将有重叠的字段。 有趣的是,您的两个代码片段在 64 位运行时都会失败,因为指针大小为 8 个字节。The common language runtime contains a verifier that makes sure the running code (verifiable IL) cannot possibly corrupt memory in the managed environment. This prevents you to declare such a structure in which fields overlap. Basically, your struct contains two data members. One integer (which is 4 bytes) and a native integer (pointer size). On a 32 bit CLR, in which you are probably running your code, the
char[]
will take 4 bytes so if you put the integer less than four bytes away from the beginning of the struct, you'll have overlapping fields. It's interesting to note that both of your code snippets with fail on a 64 bit runtime, as the pointer size is 8 bytes.我想我应该用我用来创建联盟的解决方案来回应——这就是我的初衷。 我使用了不安全的结构和固定数组,然后使用属性与固定数组进行交互。 我相信这应该能达到我想要的效果。
I figured I'd respond with the solution I used to create the union -- which was my original intention. I used an unsafe struct and a fixed array and then used a property to interact with the fixed array. I believe this should do what I want.