Convert.ToBoolean 失败并显示“0"价值 [英] Convert.ToBoolean fails with "0" value
问题描述
我正在尝试将值 "0"
( System.String
) 转换为它的 Boolean
表示,例如:
I'm trying to convert the value "0"
( System.String
) to its Boolean
representation, like:
var myValue = Convert.ToBoolean("0"); // throwing an exception here
我查看了 MSDN 页面,在代码示例块中,我找到了这些行:
I've looked at the MSDN page, and in the code-sample block, I found these lines:
ConvertToBoolean("0");
// ...
Unable to convert '0' to a Boolean.
在我的代码中,我像这样从 System.String
转换为 Boolean
:
In my code, I'm converting from the System.String
to Boolean
like this:
// will be OK, but ugly code
var myValue = Convert.ToBoolean(Convert.ToInt32("0"));
- 有没有其他方法可以转换为
Boolean
类型而不用这么难看的代码? - 为什么会出现这样的异常?因为从引用类型
System.String
转换为值类型System.Boolean
,但是System.Int32
也是值类型,不是吗? - Is there any other way to convert to the
Boolean
type with not such ugly code? - Why does such an exception occur? Because of converting from the reference type
System.String
to the value type theSystem.Boolean
, butSystem.Int32
is also a value type, isn't it? "True"
(字符串)=true
"False"
(字符串)=false
0
(数字类型;int、double、float 等)=false
- 任何非
0
(数字类型;...)=true
null
=false
"True"
(String) =true
"False"
(String) =false
0
(numerical type; int, double, float, etc.) =false
- Any non-
0
(numerical type; ...) =true
null
=false
推荐答案
发生这种情况是因为 Convert.ToBoolean
期待以下之一:
This is happening because Convert.ToBoolean
is expecting one of the following:
任何other值对于Boolean
都是无效的.
Any other value is invalid for Boolean
.
您已经有了一个干净的方法:
var myValue = Convert.ToBoolean(Convert.ToInt32("0"));
<小时>
您可以创建一个扩展方法来处理其中一些情况,同时隐藏处理转换的丑陋之处.
You can create an extension method that will handle a few of these cases for you, while hiding away the ugliness of handling the conversion.
这个扩展提供了对Boolean
的非常松散的解释:
This extension provides a very loose interpretation of Boolean
:
"True"
(String) =true
"False"
(String) =false
"0"
(String) =false
- 任何其他字符串 =
true
"True"
(String) =true
"False"
(String) =false
"0"
(String) =false
- Any other string =
true
代码:
public static class Extensions
{
public static Boolean ToBoolean(this string str)
{
String cleanValue = (str ?? "").Trim();
if (String.Equals(cleanValue, "False", StringComparison.OrdinalIgnoreCase))
return false;
return
(String.Equals(cleanValue, "True", StringComparison.OrdinalIgnoreCase)) ||
(cleanValue != "0");
}
}
<小时>
或者,如果您想要更严格的方法,遵循 .NET Framework 的要求;然后只需使用 try/catch
语句:
public static class Extensions
{
public static Boolean ToBoolean(this string str)
{
try
{
return Convert.ToBoolean(str);
}
catch { }
try
{
return Convert.ToBoolean(Convert.ToInt32(str));
}
catch { }
return false;
}
}
尽管不是 clean 或 pretty 方法,但它保证了获得正确值的更多可能性.而且,Extensions
类隐藏在您的数据/业务代码之外.
Albeit, not a clean or pretty approach, but it guarantees more possibilities of getting the correct value. And, the Extensions
class is tucked away from your data/business code.
最后,你的转换代码使用起来比较简单:
In the end, your conversion code is relatively simple to use:
String myString = "1";
Boolean myBoolean = myString.ToBoolean();
这篇关于Convert.ToBoolean 失败并显示“0"价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!