最原始的:
mystruct mys;
mys.a = array[0];
mys.b = array[1];

解决方案 »

  1.   

    我是觉得肯定有什么函数可以直接转的,因为我的struct是格式化的,上面写的较简单,其实如下
           [StructLayout(LayoutKind.Sequential, Pack=4)]
    public struct union2
    {
                               [MarshalAs(UnmanagedType.ByValArray,SizeConst=2)]
    public System.Byte[] arra1; [MarshalAs(UnmanagedType.ByValArray,SizeConst=2)]
    public System.Byte[] arra2;

    }我想这种格式化好的类,C#肯定会提供某种通用的方式(如某个函数)把同样长度的内存数据拷贝进去,对吗?
      

  2.   

    struct mystruct
    {
       public int a;
       public int b;
       
       // int array to mystruct.
       public static implicit operator mystruct(int[] args)
       {
         for (int i=0; i<args.Length; i++)
         {
              mystruct v;
              v.a = args[i];
              v.b = args[i+1]; 
         }
         return v;
       }
       
       // mystruct to int array.
    }
      

  3.   

    sorry.
    struct mystruct
    {
       public int a;
       public int b;
       
       // int array to mystruct.
       public static implicit operator mystruct(int[] args)
       {
              mystruct v;
              v.a = args[0];
              v.b = args[1]; 
          return v;
       }
       
       // mystruct to int array.
    }
      

  4.   

    像MSDN中的这个函数
    Marshal.PtrToStructure
    将数据从非托管内存块封送到托管对象

    我现在需要的就是这一类的函数,从“非托管内存块”封送到托管对象 --》从“数组“封送到托管对象
      

  5.   

    把数组数据copy到非托管内存块(函数Marshal.×××不好意思,忘了,你查一下就知道了),
    再从该内存函数copy到结构中(函数Marshal.PtrToStructure)