Convert.ToBoolean失败" 0 QUOT;值 [英] Convert.ToBoolean fails with "0" value

查看:157
本文介绍了Convert.ToBoolean失败" 0 QUOT;值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想转换值0 System.String )将其布尔表示,如:

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 布尔是这样的:

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"));




  • 是否有任何其他方式转换为布尔有没有这样丑陋的代码类型?

  • 为什么这样的异常发生的?因为从引用类型转换的 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 the System.Boolean, but System.Int32 is also a value type, isn't it?
    • 推荐答案

      这是发生,因为 Convert.ToBoolean 期待下列之一:

      This is happening because Convert.ToBoolean is expecting one of the following:

      • "True" (String) = true
      • "False" (String) = false
      • 0 (numerical type; int, double, float, etc.) = false
      • Any non-0 (numerical type; ...) = true
      • null = false

      所有的其他的值是布尔无效。

      您已经已经有了一个干净办法:

      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.

      这扩展提供了非常宽松的解读布尔

      This extension provides a very loose interpretation of Boolean:


      • (字符串)= 真正

      • (字符串) =

      • 0(字符串)=

      • 任何其他字符串= 真正

      • "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框架所期待的;然后只需使用的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;
          }
      }
      



      虽然,不是的清洁非常的办法,但它保证得到正确的价值更多的可能性。而且,扩展类是从你的数据/业务代码隐藏起来。

      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 QUOT;值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆