解析布尔C#区域性verdadero

本文关键字:区域性 verdadero 布尔 | 更新日期: 2023-09-27 18:22:05

我正试图运行这一行,但它不起作用。有人知道原因吗?

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

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

解析布尔C#区域性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看起来完全被忽略了。

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