c# - 解析 boolean C# 文化 verdadero

标签 c# xml-parsing boolean cultureinfo

我正在尝试运行这条确切的线,但它不工作。有谁知道原因吗?

Convert.ToBoolean("verdadero", new System.Globalization.CultureInfo("ES-MX"));

我正在从一个安装了多种语言的程序生成的 xml 文件中解析它,因此它将在“EN-US”文化中使用“true”或在“ES-MX”中使用“verdadero”。

最佳答案

很有趣。通过反编译器运行 Convert.ToBoolean 会发出以下信息:

/// <summary>
/// Converts the specified string representation of a logical value to its Boolean equivalent, using the specified culture-specific formatting information.
/// </summary>
/// 
/// <returns>
/// true if <paramref name="value"/> equals <see cref="F:System.Boolean.TrueString"/>, or false if <paramref name="value"/> equals <see cref="F:System.Boolean.FalseString"/> or null.
/// </returns>
/// <param name="value">A string that contains the value of either <see cref="F:System.Boolean.TrueString"/> or <see cref="F:System.Boolean.FalseString"/>. </param><param name="provider">An object that supplies culture-specific formatting information. This parameter is ignored.</param><exception cref="T:System.FormatException"><paramref name="value"/> is not equal to <see cref="F:System.Boolean.TrueString"/> or <see cref="F:System.Boolean.FalseString"/>. </exception><filterpriority>1</filterpriority>
[__DynamicallyInvokable]
public static bool ToBoolean(string value, IFormatProvider provider)
{
  if (value == null)
    return false;
  else
    return bool.Parse(value);
}

这使得 IFormatProvider 看起来完全被忽略了。

我很想说这是框架中的一个错误,但经验告诉我,当我得出这个结论时,我通常会遗漏一些东西......

关于c# - 解析 boolean C# 文化 verdadero,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17602168/

相关文章:

android - 共享首选项和 boolean 值

c# - C#汇编中的SQL汇编错误

xml - 用多个带前缀的 namespace 构造XML信封

linux - Windows 与 Linux std::getline 字符串

java - 表示一组整数的 boolean 值数组

使用 filter_var 的 PHP 验证 boolean 值

c# - 释放 COM 对象的正确方法?

c# - 关于锁性能/使用的 2 个问题

c# - 在调用 Read() 之前尝试访问字段无效,但在 C# 中调用了 Read()

java - 我什么时候应该选择 SAX 而不是 StAX?