C# 如何共享内存映射文件中的列表值

C# 如何共享内存映射文件中的列表值,c#,.net,windows,c#-4.0,memory-mapped-files,C#,.net,Windows,C# 4.0,Memory Mapped Files,我有一个带有datagridview的windows窗体,我正在将特定的列值读取到列表中。我需要在单个内存映射文件中共享列表的所有值,但以下是我关心的问题: 1.查找以字节为单位的列表大小。 2.需要共享列表中的所有项目 这是我的示例代码,我在其中共享单个变量值: string MyName = "Seema"; int totalBytes = MyName.Length * sizeof(Char) + 4; public List<string> myList =

我有一个带有datagridview的windows窗体,我正在将特定的列值读取到列表中。我需要在单个内存映射文件中共享列表的所有值,但以下是我关心的问题: 1.查找以字节为单位的列表大小。 2.需要共享列表中的所有项目

这是我的示例代码,我在其中共享单个变量值:

  string MyName = "Seema";
  int totalBytes = MyName.Length * sizeof(Char) + 4;
  public List<string> myList = new List<string>();

MemoryMappedFile MyText = MemoryMappedFile.CreateOrOpen("MyGlobalData", howManyBytes);
                byte[] array1 = new byte[howManyBytes];
                array1 = GetBytes(Name);

                using (var accessor = MyText.CreateViewAccessor(0, array1.Length))
                {
                    accessor.WriteArray(0, array1, 0, array1.Length);
                }


 static byte[] GetBytes(string str)
        {
            byte[] bytes = new byte[str.Length * sizeof(char)];
            System.Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
            return bytes;
        }
string MyName=“Seema”;
int totalBytes=MyName.Length*sizeof(Char)+4;
public List myList=new List();
MemoryMappedFile MyText=MemoryMappedFile.CreateOrOpen(“MyGlobalData”,多少字节);
字节[]数组1=新字节[多少字节];
array1=GetBytes(名称);
使用(var accessor=MyText.CreateViewAccessor(0,array1.Length))
{
accessor.WriteArray(0,array1,0,array1.Length);
}
静态字节[]获取字节(字符串str)
{
byte[]bytes=新字节[str.Length*sizeof(char)];
System.Buffer.BlockCopy(str.ToCharArray(),0,bytes,0,bytes.Length);
返回字节;
}
假设mylsit有第1项。苹果2。芒果3。菠萝


请指导我如何继续执行上述代码

您将需要使用锁定(互斥),并希望将数组大小存储为mmf中的第一个元素

使用多种序列化程序(XML、JSON、二进制等)中的一种来创建共享数据比自己重新创建要容易得多。。。你能澄清一下你试图通过手动写出每个元素来实现什么吗?只是好奇,你为什么要使用内存映射文件?您是否正在与其他流程进行通信?