目标数组不够长。检查destIndex和length,以及数组的下限。 WPF

问题描述 投票:-1回答:2
Array.Copy(Wh_PhnNum_List, this.Wh_PhnNum_List, Wh_PhnNum_List.Length); This line show a exception below..

错误:mscorlib.dll中发生了'System.ArgumentException'类型的未处理异常附加信息:目标数组不够长。检查destIndex和length,以及数组的下限。

我的代码在下面:

[StructLayout(LayoutKind.Sequential, Pack = 0)]
            public struct st_SOTA_Authen_Config
            {
                char[] Wh_PhnNum_List;

                ushort payload_length;

                //CONSTRUCTOR
                public st_SOTA_Authen_Config (char[] Wh_PhnNum_List)
                {
                    this.Wh_PhnNum_List = new char[CommandDefine.MAX_SIZE.MAX_PHN_NUM_STR_SZ];

                    Common.InitToZero(ref this.Wh_PhnNum_List);

                    Array.Copy(Wh_PhnNum_List, this.Wh_PhnNum_List, Wh_PhnNum_List.Length);

                    this.payload_length =  CommandDefine.MAX_SIZE.MAX_WH_PHNNUM_LIST_STR_SZ + (sizeof(byte) *3);
                }

为什么发生此异常?如何解决问题??

c# wpf
2个回答
0
投票

您的参数Wh_PhnNum_List必须大于设置为CommandDefine.MAX_SIZE.MAX_PHN_NUM_STR_SZ的参数。您应该将第一行替换为this.Wh_PhnNum_List = new char[Wh_PhnNum_List.Length]


0
投票
//Method to Convert the Structure to byte array
            public byte[] ToByteArray()
            {
                byte[] b = new byte[LENGTH];

                byte[] Wh_PhnNum_List = new byte[CommandDefine.MAX_SIZE.MAX_WH_PHNNUM_LIST_STR_SZ];

                Wh_PhnNum_List = Common.CharArrayToByteArray(this.Wh_PhnNum_List);

                Buffer.BlockCopy(Wh_PhnNum_List, 0, b, 0, CommandDefine.MAX_SIZE.MAX_WH_PHNNUM_LIST_STR_SZ);
                return b;
            }

此行下面显示异常

Buffer.BlockCopy(Wh_PhnNum_List, 0, b, 0, CommandDefine.MAX_SIZE.MAX_WH_PHNNUM_LIST_STR_SZ);
© www.soinside.com 2019 - 2024. All rights reserved.