未处理的异常在c#中,目标数组不够长,无法复制集合中的所有项

未处理的异常在c#中,目标数组不够长,无法复制集合中的所有项,c#,C#,我是c#初学者,试图读取二进制文件以计算二进制文件中符号的频率。(频率是符号重复的时间数) 在我采取的第一步中,我将“symbol”的数据类型保持为“int”,并且代码运行良好。 但是现在我想让这个符号成为通用类型(我的意思是类型) 代码编译时不会在ubantu终端中出现任何错误 但是当我使用“mono filename.exe BinaryFile.bin”执行时,这个二进制文件是在唯一参数处读取的。最后请看看我是如何得到这个二进制文件的。toto.bin。 错误是: hp@ubuntu:~/

我是
c#
初学者,试图读取二进制文件以计算
二进制文件中
符号的
频率。(频率是符号重复的时间数)

在我采取的第一步中,我将
“symbol”
数据类型保持为
“int”
,并且代码运行良好。 但是现在我想让这个符号成为
通用类型
(我的意思是
类型)

代码编译时不会在ubantu终端中出现任何错误

但是当我使用
“mono filename.exe BinaryFile.bin”
执行时,这个二进制文件是在
唯一参数处读取的。最后请看看我是如何得到这个二进制文件的。toto.bin。

错误是:

hp@ubuntu:~/Desktop/Internship_Xav/templatescplus$ mono test.exe toto.bin 

Unhandled Exception: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
  at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in <filename unknown>:0 
  at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in <filename unknown>:0 
  at shekhar_final_version_Csharp.Huffman`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in <filename unknown>:0 
  at shekhar_final_version_Csharp.MyClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
[ERROR] FATAL UNHANDLED EXCEPTION: System.ArgumentException: Destination array is not long enough to copy all the items in the collection. Check array index and length.
  at System.BitConverter.PutBytes (System.Byte* dst, System.Byte[] src, Int32 start_index, Int32 count) [0x00000] in <filename unknown>:0 
  at System.BitConverter.ToInt64 (System.Byte[] value, Int32 startIndex) [0x00000] in <filename unknown>:0 
  at shekhar_final_version_Csharp.Huffman`1[System.Int64]..ctor (System.String[] args, System.Func`3 converter) [0x00000] in <filename unknown>:0 
  at shekhar_final_version_Csharp.MyClass.Main (System.String[] args) [0x00000] in <filename unknown>:0 
hp@ubuntu:~/Desktop/
hp@ubuntu:~/Desktop/interporation\u Xav/templatescplus$mono test.exe toto.bin
未处理的异常:System.ArgumentException:目标数组不够长,无法复制集合中的所有项。检查数组索引和长度。
在System.BitConverter.PutBytes处(System.Byte*dst,System.Byte[]src,Int32开始索引,Int32计数)[0x00000]in:0
在System.BitConverter.ToInt64(System.Byte[]值,Int32 startIndex)[0x00000]中:0
在shekhar_最终版本_Csharp.Huffman`1[System.Int64]…ctor(System.String[]args,System.Func`3转换器)[0x00000]中:0
在shekhar_最终版本_Csharp.MyClass.Main(System.String[]args)[0x00000]中:0
[错误]致命的未处理异常:System.ArgumentException:目标数组不够长,无法复制集合中的所有项。检查数组索引和长度。
在System.BitConverter.PutBytes处(System.Byte*dst,System.Byte[]src,Int32开始索引,Int32计数)[0x00000]in:0
在System.BitConverter.ToInt64(System.Byte[]值,Int32 startIndex)[0x00000]中:0
在shekhar_最终版本_Csharp.Huffman`1[System.Int64]…ctor(System.String[]args,System.Func`3转换器)[0x00000]中:0
在shekhar_最终版本_Csharp.MyClass.Main(System.String[]args)[0x00000]中:0
hp@ubuntu:~/桌面/
我的完整代码是(缩小代码):

使用系统;
使用System.IO;
使用System.Collections.Generic;
使用System.Linq;
使用系统文本;
使用System.Runtime.InteropServices;
名称空间终结符
{
公共类A其中T:struct,i可比,i可比
{
公共类节点
{
公共T符号;
公共节点下一步;
公共国际频率;
}公共节点前端;
公共A(字符串[]args,Func转换器)
{    
int size=Marshal.SizeOf(typeof(T));
Front=null;
使用(var stream=newbinaryreader(System.IO.File.OpenRead(args[0]))
{   
while(stream.BaseStream.PositionObjSym=新的A(args,BitConverter.ToInt64);
}
}
}

我认为问题是在
公共类MyClass
中创建Huffman类型的对象时产生的。有人能帮我解决这个问题吗(任何一段代码都非常感谢)?谢谢

如果文件大小不是8个字节(64位),最后一个
ReadBytes()
将导致
byte[]
小于8个字节,从而导致
ToInt64()
失败。

请将您的代码缩小到您认为与问题相关的部分。@opd感谢您指出我刚刚做了这件事:)一般注意:尽量不要在构造函数中做太多工作。另外,命令行变量(
args
)应该在
Main
中解析。如果您事先知道
args[0]
是一个文件名,那么
Huffman
第一个参数应该是
string filename
。请少用一点
inline code
突出显示随机单词,这确实会降低问题的可读性。无论如何,请尝试再次大大减少代码。将其简化为最小的示例,使其不具有复制问题的外部依赖项(即我们无法读取的文件)。@CodeCaster好的,谢谢,我会处理它。我再次缩减了代码。上次我使用“Symbol”作为“int”类型,它工作得很好。(int在c#中是4字节),因此,我认为该文件每次读取4字节。那么,我应该如何改变我的代码与之对应呢?这是一个设计问题,而不是代码问题。处理将二进制数据强制转换为泛型类型的方法表明,您也能够理解这个问题。用零字节填充最后一个
字节[]
,使其适合
Int64
,怎么样?这完全取决于你对结果做了什么。
using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;

namespace Final 
{
    public class A < T > where T: struct, IComparable < T > , IEquatable < T > 
    {
        public class Node 
        {
            public T symbol;
            public Node next;
            public int freq;
        } public Node Front;

        public A(string[] args, Func < byte[], int, T > converter) 
        {    
            int size = Marshal.SizeOf(typeof (T));
            Front = null;
            using(var stream = new BinaryReader(System.IO.File.OpenRead(args[0]))) 
            {   
                while (stream.BaseStream.Position < stream.BaseStream.Length) 
                {   
                    byte[] bytes = stream.ReadBytes(size);   
                    T processingValue = converter(bytes, 0);  

                    Node pt, temp;
                    pt = Front;

                    while (pt != null) 
                    {   Console.WriteLine("check1");
                        if (pt.symbol.Equals(processingValue)) 
                        {
                            pt.freq++;
                            break;
                        }
                        Console.WriteLine("Symbol : {0}  frequency is : {1}", pt.symbol, pt.freq);
                        temp = pt;
                        pt = pt.next;
                    }
                }
            }
        }
    }    //////////////////////////////////////////////////
    public class MyClass 
    {
        public static void Main(string[] args) 
        {
            A < long > ObjSym = new A < long > (args, BitConverter.ToInt64);
        }
    }
}