为什么不反对有一个接受的IFormatProvider超载? [英] Why doesn't object have an overload that accepts IFormatProvider?
问题描述
当转换为实例的小数
到字符串
,使用的CultureInfo。 InvariantCulture的
并传递作为的IFormatProvider
。但是,为什么是这个重载不是在对象
一个很好的实现是:
公共虚拟字符串的ToString()
{
// yadayada,平常的ToString
}
酒店的公共虚拟字符串的ToString(的IFormatProvider提供商)
{
返回的ToString();
}
这会造成任何损害或受益于对象
类,但对象从中获得可以代替重载过载,这将是一个更容易调用它,当你不确定的类型。
这让我遇到这个问题时,我正在做一个会得到一个类的所有属性,并将其写入到XML的方法了。由于我没有要检查的对象的类型,我只是叫的ToString
。不过,如果这是一个小数,输出将根据的CurrentCulture
的线程,这不是最佳的。我可以看到的唯一的解决方法是改变的CurrentCulture
到 InvariantCulture的
,然后改变它回到不管它是什么了。但是这也只是丑陋的,因为我会写尝试finally块等。
我当前的代码是:
<在typeof运算pre>
的foreach(VAR财产(订购).GetProperties(BindingFlags.Public | BindingFlags.Instance)
其中(C =方式> ValidTypes.Contains(c.PropertyType) ))
{
VAR值= property.GetValue(订单,NULL);
如果(!值= NULL)
{
writer.WriteElementString(property.Name,
value.ToString());
}
}
不过,我希望它是:
的foreach(typeof运算中的VAR财产(订购).GetProperties(BindingFlags.Public |。BindingFlags.Instance)
其中(C = > ValidTypes.Contains(c.PropertyType)))
{
VAR值= property.GetValue(订单,NULL);
如果(!值= NULL)
{
writer.WriteElementString(property.Name,
value.ToString(CultureInfo.InvariantCulture));
}
}
没有这个超负荷的任何好处对象
?
试着投你的值
到 IFormattable
:
的foreach(在VAR财产typeof运算(顺序).GetProperties(BindingFlags.Public | BindingFlags.Instance)
其中(C => ValidTypes.Contains(c.PropertyType))。)
{
VAR值=性能。的GetValue(订单,NULL);
如果(值!= NULL)
{
VAR formattable =值IFormattable;
writer.WriteElementString(?property.Name,
formattable == NULL value.ToString():formattable.ToString(NULL,CultureInfo.InvariantCulture));
}
}
When converting for instance a decimal
to a string
, you use the CultureInfo.InvariantCulture
and pass it as an IFormatProvider
. But why is this overload not in object
?
A nice implementation would be:
public virtual string ToString()
{
// yadayada, usual ToString
}
public virtual string ToString(IFormatProvider provider)
{
return ToString();
}
This would cause no harm or benefit to the object
class, but objects deriving from it can instead override the overload and it will be a lot easier to call it when you are unsure of the type.
The problem that made me run into this was when I was making a method that would be getting all properties of a class and writing it to xml. As I didn't want to check the type of the object, I just called ToString
. But would this have been a decimal, the output would be based on the CurrentCulture
of the thread, which is not optimal. The only workaround I can see is changing the CurrentCulture
to InvariantCulture
and then changing it back to whatever it was before. But that would just be ugly as I would have to write try finally blocks etc.
My current code is:
foreach (var property in typeof(Order).GetProperties(BindingFlags.Public | BindingFlags.Instance).
Where(c => ValidTypes.Contains(c.PropertyType)))
{
var value = property.GetValue(order, null);
if (value != null)
{
writer.WriteElementString(property.Name,
value.ToString());
}
}
But I would want it to be:
foreach (var property in typeof(Order).GetProperties(BindingFlags.Public | BindingFlags.Instance).
Where(c => ValidTypes.Contains(c.PropertyType)))
{
var value = property.GetValue(order, null);
if (value != null)
{
writer.WriteElementString(property.Name,
value.ToString(CultureInfo.InvariantCulture));
}
}
Any benefit of not having this overload on object
?
Try to cast your value
to IFormattable
:
foreach (var property in typeof(Order).GetProperties(BindingFlags.Public | BindingFlags.Instance).
Where(c => ValidTypes.Contains(c.PropertyType)))
{
var value = property.GetValue(order, null);
if (value != null)
{
var formattable = value as IFormattable;
writer.WriteElementString(property.Name,
formattable == null ? value.ToString() : formattable.ToString(null, CultureInfo.InvariantCulture));
}
}
这篇关于为什么不反对有一个接受的IFormatProvider超载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!