So it appears that Convert.ToBoolean(string, IFormatProvider)
completely ignores the given IFormatProvider
.
ReferenceSource seems to agree:
public static bool ToBoolean(String value, IFormatProvider provider) {
if (value == null)
return false;
Boolean.Parse(value);
}
but, despite this, the documentation advertises the behavior one would expect. No, this is not true: as @tyranid pointed out, I'm going blind.
Digging further, there exists the Convert.ToBoolean(object, IFormatProvider)
overload, which invokes IConvertible.ToBoolean(IFormatProvider)
on the given value. Bingo, right? Just box the value and you're golden.
Sadly, though, System.String
's implementation of that method is as follows:
bool IConvertible.ToBoolean(IFormatProvider provider) {
return Convert.ToBoolean(this, provider);
}
so we're back to where we started.
This seems like a huge oversight in a framework that otherwise is generally coherent.
Maybe it's a harder problem that one would imagine?
More importantly: do any of you know of a workaround? Is one supposed to handle all the different possible truthy and falsy values with a huge switch
?
Oddly enough, either my Google-fu is getting rusty, or this issue has not been debated much.