[英]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.