How to access members of an struct like an array in C#?

前端 未结 5 1257
余生分开走
余生分开走 2021-01-26 16:16

Lets say I have a struct with more than hundred elements with complex names. And I am passing a struct of the struct type described to a function using ref, like this:



        
5条回答
  •  渐次进展
    2021-01-26 17:00

    I will probably burn in hell, but...

    Obviously horrible and not recommended and only works if your fields are all ints with default layout...

    internal class Program
    {
        private struct MyStruct
        {
            //Must be all Int32
            public int Field1, Field2, Field3;
        }
    
        private static void Main()
        {
            MyStruct str = new MyStruct {Field1 = 666, Field2 = 667, Field3 = 668};
    
            int[] array = ToArray(str);
    
            array[0] = 100;
            array[1] = 200;
            array[2] = 300;
    
            str = FromArray(array);
        }
    
        private static int[] ToArray(MyStruct str)
        {
            IntPtr ptr = IntPtr.Zero;
    
            try
            {
                int size = GetInt32ArraySize(str);
                int[] array = new int[size];
                ptr = Marshal.AllocHGlobal(size);
                Marshal.StructureToPtr(str, ptr, true);
                Marshal.Copy(ptr, array, 0, size);
                return array;
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }
    
        private static MyStruct FromArray(int[] arr)
        {
            IntPtr ptr = IntPtr.Zero;
    
            try
            {
                MyStruct str = new MyStruct();
                int size = GetInt32ArraySize(str);
                ptr = Marshal.AllocHGlobal(size);
                Marshal.Copy(arr, 0, ptr, size);
                str = (MyStruct)Marshal.PtrToStructure(ptr, str.GetType());
                return str;
            }
            finally
            {
                Marshal.FreeHGlobal(ptr);
            }
        }
    
        private static int GetInt32ArraySize(MyStruct str)
        {
            return Marshal.SizeOf(str) / Marshal.SizeOf(typeof(Int32));
        }
    }
    

提交回复
热议问题