解析布尔C#文化verdadero [英] Parse Boolean C# Culture verdadero

查看:80
本文介绍了解析布尔C#文化verdadero的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试运行此确切的行,但是行不通.有人知道原因吗?

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

我正在从安装了多种语言的程序生成的xml文件中对此进行解析,因此它将在"EN-US"文化中使用"true",在"ES-MX"中使用"verdadero".

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".

推荐答案

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

Interesting. Running Convert.ToBoolean through a decompiler emits this:

/// <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.

This makes it look like the IFormatProvider is completely disregarded.

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

I'm tempted to say it's a bug in the framework, but experience has taught me that I'm usually missing something when I come to that conclusion...

这篇关于解析布尔C#文化verdadero的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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