【问题标题】:Convert C# bytes array function to vb.net将 C# 字节数组函数转换为 vb.net
【发布时间】:2018-01-19 21:17:25
【问题描述】:

我正在尝试转换此代码

public static byte[] NewLine(this byte[] bytes, int feeds = 1)
    {
      return bytes.AddBytes(((IEnumerable<byte>) new byte[feeds]).Select<byte, byte>((Func<byte, byte>) (x => (byte) 10)).ToArray<byte>());
    }

在线转换器生产这个

<System.Runtime.CompilerServices.Extension> _
    Public Function NewLine(ByVal bytes() As Byte, Optional ByVal feeds As Integer = 1) As Byte()
      Return bytes.AddBytes((DirectCast(New Byte(feeds - 1){}, IEnumerable(Of Byte))).Select(Of Byte, Byte)CType(Function(x) CByte(10), Func(Of Byte, Byte)).ToArray())
    End Function

报错

重载解析失败,因为没有可访问的“选择”接受这个 类型参数的数量。

我该如何解决这个问题?

【问题讨论】:

  • 你有 System.Linq 的导入吗?
  • 是的,这就是为什么它说no select accepts ...
  • 这是将10 添加到集合的一种非常复杂的方式。请注意,feeds 的值被忽略,并且一直添加 10。我没有看到 CType 这个转换器 converter.telerik.com
  • 我用那个转换器&lt;System.Runtime.CompilerServices.Extension()&gt; _ Public Function NewLine2(ByVal bytes As Byte(), Optional ByVal feeds As Integer = 1) As Byte() Return bytes.AddBytes(DirectCast(New Byte(feeds - 1) {}, IEnumerable(Of Byte)).[Select](Of Byte, Byte)(DirectCast(Function(x) CByte(10), Func(Of Byte, Byte))).ToArray(Of Byte)()) End Function得到了这个,它仍然有DirectCast,它仍然给出了上面的错误

标签: c# vb.net type-conversion


【解决方案1】:

我不清楚为什么指定类型参数会失败,但无论如何都不需要 - 而且CType 的位置似乎被破坏了。如果您将Select 调用更改为:

.Select(CType(Function(x) CByte(10), Func(Of Byte, Byte)))

然后它编译 - 但你也可以完全摆脱 CType:

.Select(Function(x) CByte(10))

(这种简化也适用于 C# 代码,其中:

.Select<byte, byte>((Func<byte, byte>) (x => (byte) 10))

可以简化为:

.Select(x => (byte) 10)

【讨论】:

    猜你喜欢
    • 2010-09-18
    • 2014-05-13
    • 1970-01-01
    • 1970-01-01
    • 2014-12-19
    • 1970-01-01
    • 2016-08-27
    • 2014-08-08
    • 2019-12-15
    相关资源
    最近更新 更多