我正在进行从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#中的有效语句,因为您不能将string与bool进行比较):
if (eToken.ToLower() == false) {
throw new Exception("An eToken is required for this kind of VPN-Permission.");
}发布于 2015-01-13 12:59:27
我编译了它并在C#中解压缩了IL;,即:
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 ( Conversions在Microsoft.VisualBasic.dll中是Microsoft.VisualBasic.CompilerServices.Conversions )
发布于 2015-01-13 13:00:04
如果eToken的值为“true”/“false”,则可以执行类型强制转换:
if (Convert.ToBoolean(eToken.ToLower())==false)
throw new Exception("An eToken is required for this kind of VPN-Permission.");
}https://stackoverflow.com/questions/27922715
复制相似问题