C# 日期扩展方法模糊调用?
我在C#中创建了一些datetime扩展方法,开始上课:C# 日期扩展方法模糊调用?,c#,datetime,C#,Datetime,我在C#中创建了一些datetime扩展方法,开始上课: public static class DateExtensions { public static DateTime WeekStartDate(this DateTime dt) { DateTime _returnDateTime = dt.AddDays(-((dt.DayOfWeek - Thread.CurrentThread.CurrentCulture.Da
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);
}
}
所有内容都会生成,但当我运行项目时,会出现一个错误,说明:
以下方法或属性之间的调用不明确:“RIMS.Extensions.DateExtensions.WeekStartDate(System.DateTime)”和“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:
我不能理解一些明显的东西,只有一个星期的开始日期方法,是什么使它模棱两可?我发布了我的全部代码,因为我尝试了一切,清理、重建、关闭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;
}
}
}
使用系统;
使用系统线程;
命名空间RIMS.Extensions
{
公共静态类扩展
{
///
///返回周开始日期。
///
///这是当前日期
///日期时间
///用法:
///DateTime dt=新的DateTime();
///DateTime weekStart=dt.WeekStartDate();
公共静态日期时间WeekStartDate(此日期时间dt)
{
DateTime _returnDateTime=dt.AddDays(-(dt.DayOfWeek-Thread.CurrentThread.CurrentCulture.DateTimeFormat.FirstDayOfWeek));
return _returnDateTime;
}
///
///返回周结束日期。
///
///这是当前日期
///日期时间
///用法:
///DateTime dt=新的DateTime();
///DateTime weekEnd=dt.WeekEndDate();
公共静态DateTime WeekEndDate(此DateTime dt)
{
返回dt.WeekStartDate().AddDays(6);
}
///
///返回月份开始日期。
///
///这是当前日期
///日期时间
///用法:
///DateTime dt=新的DateTime();
///DateTime monthStart=dt.MonthStartDate();
公共静态日期时间MonthStartDate(此日期时间dt)
{
返回新的日期时间(日期年、日期月、1);
}
///
///返回月末日期。
///
///这是当前日期
///日期时间
///用法:
///DateTime dt=新的DateTime();
///DateTime monthEnd=dt.MonthEndDate();
公共静态DateTime MonthEndDate(此DateTime dt)
{
return DateTime.Now.MonthStartDate().AddDays(DateTime.DaysInMonth(dt.Year,dt.Month)-1);
}
///
///返回日期是否在周末
///
///这是当前日期
///布尔值
///用法:
///DateTime dt=新的DateTime();
///bool-itsawekend=dt.IsWeekend();
公共静态bool IsWeekend(此日期时间值)
{
return(value.DayOfWeek==DayOfWeek.Sunday | | value.DayOfWeek==DayOfWeek.Saturday);
}
///
///返回日期是否介于开始日期和结束日期之间
///
///这是当前日期
///要比较的开始日期
///要比较的结束日期
///可选bool是否比较时间
///布尔值
///用法:
///DateTime dt=新的DateTime();
///布尔值isBetween=dt.isBetween(someStartDate,someEndDate);
公共静态bool IsBetween(此DateTime dt、DateTime startDate、DateTime endDate、布尔compareTime=false)
{
返回比较时间?
dt>=startDate&&dt=startDate.Date&&dt.Date为了确保没有两个同名的方法,只需在扩展名中重命名WeekStartDate,看看它是否可以编译,它编译时没有错误。这意味着在其他地方有相同的方法,然后右键单击它,选择“转到定义”。I我重复了这个问题。
我创建了一个WinForm项目并添加了您的扩展类。只有一个项目。
编译,然后我从bin\Debug文件夹向exe文件添加了一个文件引用,运行应用程序,得到了相同的异常。
编辑:
复制Web项目的步骤并获得相同的结果。
好吧……我明白了它不起作用的原因。我在App_code文件夹中有扩展代码文件夹。我从中删除它的那一刻它就起作用了
抱歉,各位!清理解决方案并重试。如果这不起作用,请关闭Visual Studio并重新打开它。我将其代码粘贴到VS中,并在编译和运行时没有错误或警告。Habib因此您不能直接从扩展类调用扩展方法?这似乎很奇怪您的项目中必须有该方法的另一个副本这里。如果有必要,请进行文本搜索。我很感兴趣,看看这篇文章是怎么写的。请阅读上面的评论…我尝试过用相同的问题重命名它们。听起来不一样-他说清除不会改变任何东西,在这种情况下,清除会破坏构建。在下一次编译中,清除不会解决此错误引用n他将得到相同的异常。如果这是问题,他必须手动删除文件引用。@Elio.Batista-哪个文件引用?这只是我项目中的一个类文件。我在我的项目中查找这个类文件的位置?在您的同一个项目@JonH中,在引用节点下查找,我很有信心您将在投射对自身的引用。这是我的问题,没有对自身的引用。如果你愿意,我可以发布一个屏幕截图?这是因为App_代码中的内容是动态编译的。这是历史性的。显然VisualWebDeveloper需要这一点才能在设计师中拥有IntelliSense。当网站的概念出现在.Net 2.0中时,你必须你所有的类都没有被代码隐藏到App_代码中。这是我对的解释。向Luke Hutton喊道,他第一个想到动态编译可能是原因。