相关疑难解决方法(0)

托管结构的大小

.NET 4.0 Framework引入了用于读取和写入内存映射文件的类.这些类以读取写入结构的方法为中心.这些不是编组的,而是以它们在托管内存中布局的形式从文件复制到文件.

假设我想使用以下方法将两个结构顺序写入内存映射文件:

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Foo
{
    public char C;
    public bool B;
}

[StructLayout(LayoutKind.Sequential, Pack = 1)]
struct Bar
{
}

static void Write<T1, T2>(T1 item1, T2 item2)
    where T1 : struct
    where T2 : struct
{
    using (MemoryMappedFile file = MemoryMappedFile.CreateNew(null, 32))
    using (MemoryMappedViewAccessor accessor = file.CreateViewAccessor())
    {
        accessor.Write<T1>(0L, ref item1);  //  <-- (1)
        accessor.Write<T2>(??, ref item2);  //  <-- (2)
    }
}

static void Main()
{
    Foo foo …
Run Code Online (Sandbox Code Playgroud)

c# .net-4.0

12
推荐指数
1
解决办法
2111
查看次数

标签 统计

.net-4.0 ×1

c# ×1