本文介绍了Convert.ToDateTime(布尔)的要点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做某种类型的转换例程昨晚的系统我工作。其中一个转换涉及转字符串值到他们的DateTime等效。

I was doing some type conversion routines last night for a system I am working on. One of the conversions involves turning string values into their DateTime equivalents.

虽然这样做,我注意到Convert.ToDateTime()方法有它接受一个布尔值参数的重载。

While doing this, I noticed that the Convert.ToDateTime() method had an overload which accepted a boolean parameter.

第一个问题?在什么情况下会这样永远是有用的?

First question? Under what circumstances could this ever be useful?

我去远一点,并试图在快速监视执行的方法。无论哪种方式(true或false),则返回一个InvalidCastException。

I went a little further and tried to execute the method in QuickWatch. Either way ( true or false ), the routine returns an InvalidCastException.

第二个问题吗?为什么这个方法即使在这里?

Second question? Why is this method even here?

修改

感谢您的答案,伙计们。我可以看到它的有道理的从一个契约点,但它似乎奇怪,核心框架包含的方法: -

Thanks for the answers, guys. I can see how it makes sense from a contractual point of view, but it does seem odd that the core framework includes methods that:-

  • 永远不能工作
  • 更糟的是,实际上将抛出一个异常,当有人试图调用它。

这是一个有点像一个人做装有控制,主动地使用时,工作车辆停一辆车。

It's a bit like someone making a car loaded with controls that actively stop your vehicle from working when used.

推荐答案

这是有道理的,因为 ToDateTime 是一部分的 IConvertible 布尔实现的接口。如果你看看在反射器,你会看到,它抛出一个 InvalidCastException的

It makes sense because ToDateTime is part of the IConvertible interface implemented by bool. If you look in reflector you will see that it throws an InvalidCastException.

更新(从转换):

public static DateTime ToDateTime(bool value)
{
    return ((IConvertible) value).ToDateTime(null);
}

这篇关于Convert.ToDateTime(布尔)的要点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-23 04:39