简体   繁体   English

将VB.Net转换为C#时出错

[英]Error when converting VB.Net to C#

I have this VB.Net function ( from other project ) which I need to convert into C# . 我有这个VB.Net函数( 来自其他项目 ),我需要将其转换为C#

Private Function Convert(ByVal Value As String) As String
    Dim transformed = Encoding.Unicode.GetBytes(Value).Select( _
        Function(item) Not item)
    Return Encoding.Unicode.GetString(transformed.ToArray())
End Function

Here's what I've tried: 这是我尝试过的:

private string Convert(string Value)
{
    return Encoding.Unicode.GetString(Enumerable.ToArray<byte>(Enumerable.Select<byte, byte>((IEnumerable<byte>)Encoding.Unicode.GetBytes(Value), 
               (Func<byte, byte>)(item => ~item))));
}

I am getting two errors: 我遇到两个错误:

  • Cannot implicitly convert type 'int' to 'byte'. 无法将类型'int'隐式转换为'byte'。 An explicit conversion exists (are you missing a cast?) 存在显式转换(您是否缺少演员表?)
  • Cannot convert lambda expression to delegate type 'System.Func' because some of the return types in the block are not implicitly convertible to the delegate return type 无法将lambda表达式转换为委托类型'System.Func',因为该块中的某些返回类型不能隐式转换为委托返回类型

Can anyone help me on this? 谁可以帮我这个事?

You can try this C# code. 您可以尝试此C#代码。

private string Convert(string Value)
{
    dynamic transformed = Encoding.Unicode.GetBytes(Value).Select(item => (byte)~item);
    return Encoding.Unicode.GetString(transformed.ToArray());
}

I have used this tool to convert - http://converter.telerik.com/ 我已使用此工具进行转换-http: //converter.telerik.com/

Try the below code. 试试下面的代码。

I am not sure why LINQ Select converts the byte array to an int array, hence the explicit cast. 我不确定LINQ Select为什么将字节数组转换为int数组,从而进行显式转换。

private string Convert(string value)
{
    var invertedBytes = Encoding.Unicode.GetBytes(value).Select(b => (byte)~b).ToArray();
    return Encoding.Unicode.GetString(invertedBytes);
}

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM