简体   繁体   English

无法将类型为“ WhereSelectArrayIterator`2 [System.String,System.Byte]”的对象强制转换为“ System.Byte []”。 Vb.net

[英]Unable to cast object of type 'WhereSelectArrayIterator`2[System.String,System.Byte]' to type 'System.Byte[]'. Vb.net

I'm trying to make a program in vb.net and what it does is that when you open a file it turns the file opened into hexadecimal code, But the problem is that when it saves and tries to convert it back to normal. 我正在尝试在vb.net中创建一个程序,它的作用是当您打开文件时将打开的文件转换为十六进制代码,但是问题是当它保存并尝试将其转换回正常状态时。 it results to a: (Unable to cast object of type 'WhereSelectArrayIterator`2[System.String,System.Byte]' to type 'System.Byte[]'.) exception. 结果为:(无法将类型为“ WhereSelectArrayIterator`2 [System.String,System.Byte]”的对象强制转换为“ System.Byte []”。)

Here's the Opening and Saving code 这是打开和保存代码

Opening file code: FillWithHex(RichTextBox1,OpenFileDialog1.FileName) 打开文件代码:FillWithHex(RichTextBox1,OpenFileDialog1.FileName)

    Async Sub FillWithHex(rtb As RichTextBox, name As String)
    For Each ctl In Controls
        ctl.Enabled = False
    Next ctl
    Dim buff(1000000) As Byte

    Using fs = New FileStream(name, FileMode.Open)
        Using br = New BinaryReader(fs)
            While True
                Dim text = String.Empty
                buff = br.ReadBytes(1000000)
                Await Task.Run(Sub() text = String.Join(" ", buff.
                            Select(Function(b) b.ToString("X2")))).
                            ConfigureAwait(True)
                rtb.AppendText(text)
                If buff.Length < 1000000 Then
                    Exit While
                End If
            End While

        End Using
    End Using
    For Each ctl In Controls
        ctl.Enabled = True
    Next ctl
    ToolStripLabel1.Text = "Status: Idle"
End Sub

And here is the saving code 这是保存代码

        Try
        Dim b As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16)))
        My.Computer.FileSystem.WriteAllBytes(SaveFileDialog1.FileName, b, False)
    Catch ex1 As Exception
        Try
            Dim b As Byte() = RichTextBox1.Text.Split(" "c).Select(Function(n) Convert.ToByte(Convert.ToInt32(n, 16)))
            My.Computer.FileSystem.WriteAllBytes(OpenFileDialog1.FileName, b, False)
        Catch ex As Exception
            MsgBox("Exception caught : " + vbNewLine + vbNewLine + ex.ToString, MsgBoxStyle.Critical, "Exception Error")
    End Try
    End Try

Extensions methods from the Enumerable class that you call on object of type IEnumerable(Of T) , like that Select method, generally don't return an array. 您对IEnumerable(Of T)类型的对象调用的Enumerable类的扩展方法IEnumerable(Of T)Select方法)通常不返回数组。 They generally return some type that implements IEnumerable(Of T) . 它们通常返回实现IEnumerable(Of T)某种类型。 The specific type generally doesn't matter. 特定类型通常无关紧要。 If you need an array then you need to call ToArray on that object. 如果需要数组,则需要在该对象上调用ToArray ToList will similarly create a List(Of T) . ToList将类似地创建一个List(Of T) That means you need this: 这意味着您需要:

Dim b = RichTextBox1.Text.
                     Split(" "c).
                     Select(Function(n) Convert.ToByte(n, 16)).
                     ToArray()

Note that I have removed the explicit type declaration, ie As Byte() , and let the type be inferred. 请注意,我删除了显式类型声明,即As Byte() ,然后推断类型。 That makes for neater code but you don't have to do that if you think that having the explicit type is helpful. 这样可以使代码更整洁,但是如果您认为使用显式类型会有所帮助,则不必这样做。 Note that I have also removed the useless Convert.ToInt32 call. 请注意,我还删除了无用的Convert.ToInt32调用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法将“System.Byte”类型的对象转换为“System.Byte[]”类型 - vb .net - Unable to cast object of type 'System.Byte' to type 'System.Byte[]' - vb .net 无法在vb.net中将类型为&#39;system.byte&#39;的对象转换为类型为&#39;system.iconvertible&#39; - Unable to cast object of type 'system.byte ' to type 'system.iconvertible in vb.net 正在下载附件。 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象 - Downloading an attachment. Unable to cast object of type 'System.String' to type 'System.Byte[]' 无法将类型为“ System.String”的对象转换为类型为“ System.Byte []”的对象。 为什么? - Unable to cast object of type 'System.String' to type 'System.Byte[]'. Why? 错误! 无法将类型为“ system.object”的对象转换为类型为“ system.byte []”的对象 - Error! unable to cast object of type 'system.object ' to type 'system.byte[]' 无法在Windows应用程序中将类型为“ System.Byte []”的对象转换为类型为“ System.Drawing.Image”的对象 - Unable to cast object of type 'System.Byte[]' to type 'System.Drawing.Image' in windows application 无法将类型为“ System.DBNull”的对象转换为类型为“ System.Byte []”的对象。 - Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'. 无法将类型为&#39;System.Byte []&#39;的对象强制转换为&#39;System.IConvertible&#39; - Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible' 无法将类型为&#39;System.dbnull&#39;的对象强制转换为&#39;system.byte []&#39; - Unable to cast object of type ' System.dbnull' to type 'system.byte[]' 将图像更新到 MySQL 数据库时,无法将类型为“System.Byte[]”的 object 转换为键入“System.IConvertible” - Unable to cast object of type 'System.Byte[]' to type 'System.IConvertible' when updating an image to MySQL database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM