固定长度字符串作为在MemoryMappedViewAccessor中使用的值类型

时间:2022-09-26 21:38:16

I would like to store a struct into a MemoryMappedFile, but this struct contains strings (with a max size which I know) that are reference type and therefore rejected by the MemoryMappedViewAccessor.

我想将一个结构存储到MemoryMappedFile中,但是这个结构包含引用类型的字符串(我知道最大大小),因此被MemoryMappedViewAccessor拒绝。

Do you know a good fixed-length string struct (value type) implementation? I can hard code the length if that is necessary (as arrays are reference types).

你知道一个很好的固定长度字符串结构(值类型)实现吗?如果有必要,我可以硬编码长度(因为数组是引用类型)。

For example, I could instantiate it as:

例如,我可以将其实例化为:

TenCharsString myString = new TenCharsString("1234567890");

Decorating with the MarshalAs attribute

使用MarshalAs属性进行装饰

<MarshalAs (UnmanagedType.ByValTStr, SizeConst:=128)> 

should work, but it does not.

应该工作,但事实并非如此。

2 个解决方案

#1


1  

Here's a simple 10-character value type:

这是一个简单的10个字符的值类型:

struct TenChars
{
    public char A;
    public char B;
    public char C;
    public char D;
    public char E;
    public char F;
    public char G;
    public char H;
    public char I;
    public char J;
}

If unsafe code is an option, this works as well:

如果不安全的代码是一个选项,这也适用:

unsafe struct TenChars
{
    public fixed char Chars[10];
}

Full code:

struct TenChars1
{
    public const int Capacity = 10;

    private char A;
    private char B;
    private char C;
    private char D;
    private char E;
    private char F;
    private char G;
    private char H;
    private char I;
    private char J;

    public TenChars1(string value)
    {
        if (value == null) throw new ArgumentNullException("value");
        if (value.Length > Capacity) throw new ArgumentException();

        A = (value.Length > 0) ? value[0] : '\0';
        B = (value.Length > 1) ? value[1] : '\0';
        C = (value.Length > 2) ? value[2] : '\0';
        D = (value.Length > 3) ? value[3] : '\0';
        E = (value.Length > 4) ? value[4] : '\0';
        F = (value.Length > 5) ? value[5] : '\0';
        G = (value.Length > 6) ? value[6] : '\0';
        H = (value.Length > 7) ? value[7] : '\0';
        I = (value.Length > 8) ? value[8] : '\0';
        J = (value.Length > 9) ? value[9] : '\0';
    }

    public override string ToString()
    {
        return new string(new char[] { A, B, C, D, E, F, G, H, I, J });
    }
}

unsafe struct TenChars2
{
    public const int Capacity = 10;

    private fixed char buffer[Capacity];

    public TenChars2(string value)
    {
        if (value == null) throw new ArgumentNullException("value");
        if (value.Length > Capacity) throw new ArgumentException();

        fixed (char* ptr = this.buffer)
        fixed (char* chars = value)
        {
            for (int i = 0; i < value.Length; i++)
            {
                *(ptr + i) = *(chars + i);
            }
        }
    }

    public override string ToString()
    {
        fixed (char* ptr = this.buffer)
        {
            return new string(ptr);
        }
    }
}

#2


0  

You can store the struct internals individually. You can store the strings as arrays of chars using WriteArray and ReadArray. The others can use the various Write methods.

您可以单独存储结构内部。您可以使用WriteArray和ReadArray将字符串存储为字符数组。其他人可以使用各种Write方法。

#1


1  

Here's a simple 10-character value type:

这是一个简单的10个字符的值类型:

struct TenChars
{
    public char A;
    public char B;
    public char C;
    public char D;
    public char E;
    public char F;
    public char G;
    public char H;
    public char I;
    public char J;
}

If unsafe code is an option, this works as well:

如果不安全的代码是一个选项,这也适用:

unsafe struct TenChars
{
    public fixed char Chars[10];
}

Full code:

struct TenChars1
{
    public const int Capacity = 10;

    private char A;
    private char B;
    private char C;
    private char D;
    private char E;
    private char F;
    private char G;
    private char H;
    private char I;
    private char J;

    public TenChars1(string value)
    {
        if (value == null) throw new ArgumentNullException("value");
        if (value.Length > Capacity) throw new ArgumentException();

        A = (value.Length > 0) ? value[0] : '\0';
        B = (value.Length > 1) ? value[1] : '\0';
        C = (value.Length > 2) ? value[2] : '\0';
        D = (value.Length > 3) ? value[3] : '\0';
        E = (value.Length > 4) ? value[4] : '\0';
        F = (value.Length > 5) ? value[5] : '\0';
        G = (value.Length > 6) ? value[6] : '\0';
        H = (value.Length > 7) ? value[7] : '\0';
        I = (value.Length > 8) ? value[8] : '\0';
        J = (value.Length > 9) ? value[9] : '\0';
    }

    public override string ToString()
    {
        return new string(new char[] { A, B, C, D, E, F, G, H, I, J });
    }
}

unsafe struct TenChars2
{
    public const int Capacity = 10;

    private fixed char buffer[Capacity];

    public TenChars2(string value)
    {
        if (value == null) throw new ArgumentNullException("value");
        if (value.Length > Capacity) throw new ArgumentException();

        fixed (char* ptr = this.buffer)
        fixed (char* chars = value)
        {
            for (int i = 0; i < value.Length; i++)
            {
                *(ptr + i) = *(chars + i);
            }
        }
    }

    public override string ToString()
    {
        fixed (char* ptr = this.buffer)
        {
            return new string(ptr);
        }
    }
}

#2


0  

You can store the struct internals individually. You can store the strings as arrays of chars using WriteArray and ReadArray. The others can use the various Write methods.

您可以单独存储结构内部。您可以使用WriteArray和ReadArray将字符串存储为字符数组。其他人可以使用各种Write方法。