日期延长方法暧昧的电话?

时间:2014-04-14 17:49:56

标签: c# datetime

我在C#中创建了一些日期时间扩展方法,开始了我的课程:

public static class DateExtensions
    { 
        public static DateTime WeekStartDate(this DateTime dt)
        {
            DateTime _returnDateTime = dt.AddDays(-((dt.DayOfWeek - Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek)));
            return _returnDateTime;
        }

 public static DateTime WeekEndDate(this DateTime dt)
        {
            return dt.WeekStartDate().AddDays(6);
        }
    }

一切都在构建,但是当我运行我的项目时,我收到一条错误说明:

The call is ambiguous between the following methods or properties: 'RIMS.Extensions.DateExtensions.WeekStartDate(System.DateTime)' and 'RIMS.Extensions.DateExtensions.WeekStartDate(System.DateTime)'

指向这个:

Line 30:         public static DateTime WeekEndDate(this DateTime dt)
Line 31:         {
Line 32:             return dt.WeekStartDate().AddDays(6);
Line 33:         }
Line 34: 

我一定不能理解一些明显的东西,只有一个WeekStartDate方法,是什么让它变得暧昧?我发布了我的整个代码,因为我已经尝试了所有东西,清理,重建,关闭vs等等:

using System;
using System.Threading;

namespace RIMS.Extensions
{
    public static class DateExtensions
    {
        /// <summary>
        /// Returns week start date.
        /// </summary>
        /// <param name="dt">this - the current date</param>
        /// <returns>datetime</returns>
        /// usage: 
        /// DateTime dt = new DateTime();
        /// DateTime weekStart = dt.WeekStartDate();
        public static DateTime WeekStartDate(this DateTime dt)
        {
            DateTime _returnDateTime = dt.AddDays(-((dt.DayOfWeek - Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek)));
            return _returnDateTime;
        }

        /// <summary>
        /// Returns week end date.
        /// </summary>
        /// <param name="dt">this - the current date</param>
        /// <returns>datetime</returns>
        /// usage: 
        /// DateTime dt = new DateTime();
        /// DateTime weekEnd = dt.WeekEndDate();
        public static DateTime WeekEndDate(this DateTime dt)
        {
            return dt.WeekStartDate().AddDays(6);
        }

        /// <summary>
        /// Returns month start date.
        /// </summary>
        /// <param name="dt">this - the current date</param>
        /// <returns>datetime</returns>
        /// usage: 
        /// DateTime dt = new DateTime();
        /// DateTime monthStart = dt.MonthStartDate();
        public static DateTime MonthStartDate(this DateTime dt)
        {
            return new DateTime(dt.Year, dt.Month, 1);
        }

        /// <summary>
        /// Returns month end date.
        /// </summary>
        /// <param name="dt">this - the current date</param>
        /// <returns>datetime</returns>
        /// usage: 
        /// DateTime dt = new DateTime();
        /// DateTime monthEnd = dt.MonthEndDate();
        public static DateTime MonthEndDate(this DateTime dt)
        {
            return DateTime.Now.MonthStartDate().AddDays(DateTime.DaysInMonth(dt.Year, dt.Month) - 1);
        }

        /// <summary>
        /// Returns whether a date falls on a weekend
        /// </summary>
        /// <param name="value">this - the current date</param>
        /// <returns>boolean</returns>
        /// usage: 
        /// DateTime dt = new DateTime();
        /// bool itsAWeekend = dt.IsWeekend();
        public static bool IsWeekend(this DateTime value)
        {
            return (value.DayOfWeek == DayOfWeek.Sunday || value.DayOfWeek == DayOfWeek.Saturday);
        }

        /// <summary>
        /// Returns whether a date falls between a start and end date
        /// </summary>
        /// <param name="dt">this - the current date</param>
        /// <param name="startDate">Beginning date to compare</param>
        /// <param name="endDate">End date to compare</param>
        /// <param name="compareTime">Optional bool whether or not to compare the time</param>
        /// <returns>boolean</returns>
        /// usage: 
        /// DateTime dt = new DateTime();
        /// Boolean isBetween = dt.IsBetween(someStartDate, someEndDate);
        public static bool IsBetween(this DateTime dt, DateTime startDate, DateTime endDate, Boolean compareTime = false)
        {
            return compareTime ?
               dt >= startDate && dt <= endDate :
               dt.Date >= startDate.Date && dt.Date <= endDate.Date;
        }
    }
}

这里也是我的参考文献清单:

enter image description here

我没有看到我的项目的参考。

3 个答案:

答案 0 :(得分:3)

嗯......我看到它无法正常工作的原因。我在App_Code文件夹中有扩展代码文件夹。我从那里删除它的那一刻就开始了!

对不起伙计们!

答案 1 :(得分:0)

我已经复制了这个问题。 我创建了一个WinForm项目并添加了你的extenssion类。只是一个项目。 编译然后我从bin \ Debug文件夹中添加了对exe文件的文件引用,运行应用程序并获得相同的异常。 编辑: 重现了Web项目的步骤并获得了相同的结果。 enter image description here enter image description here

答案 2 :(得分:-1)

为了确保您没有2个具有相同名称的方法,只需重命名扩展中的WeekStartDate并查看它是否可以编译,它编译时没有错误,这意味着您有相同的方法在其他地方,然后右键单击它选择转到定义。

相关问题