如何计算此 VB.NET 代码

本文关键字:VB NET 代码 计算 何计算 | 更新日期: 2023-09-27 18:37:24

我正在研究从 VB.NET 到C#的代码转换。我找到了这段代码,我试图绕开它,但我无法理解它是如何计算的:

If eToken.ToLower = False Then _
    Throw New Exception("An eToken is required for this kind of VPN-Permission.)

我的问题是字符串eToken.ToLower和布尔值False之间的比较。

尝试使用转换器,我得到的如下(这在 C# 中不是有效的语句,因为您无法将stringbool进行比较):

if (eToken.ToLower() == false) {
    throw new Exception("An eToken is required for this kind of VPN-Permission.");
}

如何计算此 VB.NET 代码

我编译了它并反编译了IL;在C#中,即:

string eToken = "abc"; // from: Dim eToken As String = "abc" in my code 
if (!Conversions.ToBoolean(eToken.ToLower()))
{
    throw new Exception("An eToken is required for this kind of VPN-Permission.");
}

所以有你的答案:它正在使用Conversions.ToBoolean(其中ConversionsMicrosoft.VisualBasic.dllMicrosoft.VisualBasic.CompilerServices.Conversions

您可以进行类型转换,假设 eToken 的值为 "true"/"false":

if (Convert.ToBoolean(eToken.ToLower())==false)
    throw new Exception("An eToken is required for this kind of VPN-Permission.");
}