如何在知道正确的数组长度之前将字节数组初始化为c#中的OUT参数

本文关键字:数组 字节数 字节 初始化 参数 OUT 中的 | 更新日期: 2023-09-27 18:06:59

我在使用webmethod将文件下载到调用HTTPHandler时遇到了麻烦。ashx文件。处理程序按如下方式调用webmethod:

byte[] docContent;
string fileType;
string fileName;
string msgInfo = brokerService.DownloadFile(trimURL, recNumber, out docContent, out fileType, out fileName);

在被调用的webmethod中,我必须在使用它之前初始化字节数组,否则我会在所有返回语句中得到编译器错误:

The out parameter 'docContents' must be assigned to before control leaves the current method

我尝试将其设置为空数组,但这会导致缓冲区。BlockCopy方法失败:

Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.
mscorlib
  at System.Buffer.BlockCopy(Array src, Int32 srcOffset, Array dst, Int32 dstOffset, Int32 count)

我知道我需要初始化它,但我不知道数组的长度,直到我访问数据库。通过调试,我已经验证了所有的代码工作,除了缓冲区。BlockCopy:

    public string DownloadFile(string trimURL
        , string TrimRecordNumber
        , out byte[] docContents
        , out string returnFiletype
        , out string returnFilename)
    {
        docContents = new byte[0];
        returnFiletype = null; returnFilename = null;
        try
        {
            ConnectToTrim(trimURL);
            if (!db.IsValid)
                return "CRITICAL: Database Is NOT Valid";
            Record myRec = db.GetRecord(TrimRecordNumber);
            if (myRec == null)
                return "CRITICAL: Record not found.";
            uint bufferSize = 10000;
            int documentLength = myRec.DocumentSize;
            byte[] result = new byte[documentLength];
            byte[] buffer = new byte[bufferSize];
            uint bytesRead;
            uint totalBytesRead = 0;
            TRIMSDK.IStream docStream = myRec.GetDocumentStream(string.Empty, false, string.Empty);
            while (totalBytesRead < documentLength)
            {
                docStream.RemoteRead(out buffer[0], 10000, out bytesRead);
                for (int i = 0; i < bytesRead; i++)
                {
                    result[totalBytesRead] = buffer[i];
                    totalBytesRead += 1;
                }
            }
            returnFiletype = myRec.Extension;
            returnFilename = myRec.SuggestedFileName;
            Buffer.BlockCopy(result, 0, docContents, 0, result.Length); 
            return string.Format("OK-Document for recordnumber({0}): {1}.{2} - size: {3} bytes",
                TrimRecordNumber, returnFilename, returnFiletype, Convert.ToString(documentLength)); 
        }
        catch (Exception ex)
        {
            return LogException(ex, "CRITICAL: Exception in DownloadFile method has been logged:", trimURL, 100);
        } 
    } 

如何在知道正确的数组长度之前将字节数组初始化为c#中的OUT参数

可以先将其初始化为null。在调用Buffer.BlockCopy之前,分配并初始化它为合适的长度。

这看起来像:

public string DownloadFile(string trimURL
    , string TrimRecordNumber
    , out byte[] docContents
    , out string returnFiletype
    , out string returnFilename)
{
    docContents = null;
    //...
        returnFiletype = myRec.Extension;
        returnFilename = myRec.SuggestedFileName;
        docContents = new byte[result.Length]; // Allocate appropriately here...
        Buffer.BlockCopy(result, 0, docContents, 0, result.Length); 
        return ...

或者,您可以直接分配结果并将其复制到docContents中-完全消除了result的需要。但是,如果您想单独保留整个流量控制,那么您仍然需要在开始时初始化到null