可空的DateTime扩展引发“不包含定义"异常 [英] Nullable DateTime extension throws 'does not contain a definition' exception

查看:89
本文介绍了可空的DateTime扩展引发“不包含定义"异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面的代码中:

void Main()
{
    DateTime cleanDate = DateTime.Now.ToCleanDateTime();    
    DateTime? nullableCleanDate = DateTime.Now;
    nullableCleanDate = nullableCleanDate.ToCleanDateTime();

    cleanDate.Dump();
    nullableCleanDate.Dump();
}

public static class Extensions
{
    //public static DateTime ToCleanDateTime(this DateTime dt)
    //{
    //  dt = new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);
    //  return dt;
    //}

    public static DateTime? ToCleanDateTime(this DateTime? dt)
    {
        if (dt.HasValue)
            dt = new DateTime(dt.Value.Year, dt.Value.Month, dt.Value.Day, 0, 0, 0, 0);
        return dt;
    }
}

此行 DateTime cleanDate = DateTime.Now.ToCleanDateTime(); 引发以下异常.

"System.DateTime"不包含"ToCleanDateTime"的定义最好的扩展方法重载'Extensions.ToCleanDateTime(System.DateTime?)'有一些无效争论

'System.DateTime' does not contain a definition for 'ToCleanDateTime' and the best extension method overload 'Extensions.ToCleanDateTime(System.DateTime?)' has some invalid arguments

如果我在 DateTime 的扩展方法中发表评论,则不会引发公共静态DateTime ToCleanDateTime(this DateTime dt)错误.

If I comment in extension method for DateTime public static DateTime ToCleanDateTime(this DateTime dt) error is not thrown.

有人可以启发我吗?

  1. 为什么会引发此错误(除了显而易见的错误)?
  2. 有没有办法将这两个扩展合并为一个方法?

我以某种方式期望公共静态DateTime吗?ToCleanDateTime(this DateTime?dt)可以同时处理 DateTime DateTime?.

I somehow expected public static DateTime? ToCleanDateTime(this DateTime? dt) to handle both DateTime and DateTime?.

推荐答案

这不是抛出的异常,而是编译器错误.前者在运行时发生,后者在编译时发生.这是重要的区别.

That's not an exception that's thrown, that's a compiler error. The former happens at runtime, the latter at compile time. It's an important difference.

出现此错误是因为编译器找不到接受 DateTime 的扩展方法,只有一个带有 DateTime?的扩展方法.这些是完全不同的类型. Nullable< T> T 没有任何继承关系,因此您不能为 Nullable< T> 接受 T ,反之亦然.

You get this error because the compiler can't find an extension method accepting a DateTime, only one with a DateTime?. Those are entirely different types. Nullable<T> has no inheritance relation whatsoever to T, so you can't make an overload for Nullable<T> accept a T, nor vice versa.

您可以通过让一个调用另一个来组合这些方法:

You can combine the methods by letting one call the other:

public static class Extensions
{
    public static DateTime ToCleanDateTime(this DateTime dt)
    {
      return new DateTime(dt.Year, dt.Month, dt.Day, 0, 0, 0, 0);      
    }

    public static DateTime? ToCleanDateTime(this DateTime? dt)
    {
        if (dt.HasValue)
        {
            return dt.Value.ToCleanDateTime();
        }
        return dt;
    }
}

或者您可以只使用 DateTime.Date ,它返回与扩展方法相同的内容,并保留 Kind ,与上述

Or you can just use DateTime.Date, which returns the same as your extension method and preserves the Kind, as opposed to the above code with which Kind will be Unspecified regardless the input's Kind.

这篇关于可空的DateTime扩展引发“不包含定义"异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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