4

I am trying to run this exact line, and it isn't working. Anyone know the reason why?

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

I am parsing this from an xml file generated by a program that has many languages installed, and so it will use "true" in "EN-US" culture or "verdadero" in "ES-MX".

4

1 回答 1

3

有趣的。通过反编译器运行 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 看起来完全被忽略了。

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

于 2013-07-11T20:00:58.927 回答