我在stackoverflow中看到了这个错误消息问题,但它们都不是日期时间或日期类型,只能使用日期类型我已经创建了一个日期类型的类,并在日期类中为它写了一些重载。我的日期类是
using System;
namespace Common
{
public class Date
{
private DateTime _d1;
public Date(DateTime dateTime)
{
_d1 = dateTime;
}
public static bool operator <(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is smaller than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result < 0)
{
flag = true;
}
return flag;
}
public static bool operator >(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result > 0)
{
flag = true;
}
return flag;
}
public static bool operator <=(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result <= 0)
{
flag = true;
}
return flag;
}
public static bool operator >=(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result >= 0)
{
flag = true;
}
return flag;
}
public static bool operator ==(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result == 0)
{
flag = true;
}
return flag;
}
public static bool operator !=(Date date1, Date date2)
{
bool flag = false;
//Now, get the original DateTime Type of C#
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
//Now compare the two DateTime variables and assign the flag to true
//if the first date is Greater than the second date
int result = DateTime.Compare(firstDate, secondDate);
if (result != 0)
{
flag = true;
}
return flag;
}
}//end of class Date
}//End of namespace
但问题是当我试图在我的代码页面后面使用它给我这个错误 - 无法将'Common.Date'类型的对象强制转换为'System.IConvertible
我正在使用它的代码 date purchaseDate = new Date(item.PurchaseDate); 提交日期日期=新日期(item.SubmissionDate);
if (purchaseDate>submissionSate)
{
//to do
}
此处在item对象中,purchaseate和submision日期是datetime属性,错误在if行中 任何人都可以提供任何建议吗?这个问题的可能解决方案是什么?
答案 0 :(得分:2)
您可以直接访问Date
的字段。虽然我质疑这个Date
对象的用处。
public static bool operator <(Date date1, Date date2)
{
return date1 != null && date2 != null && date1._d1 < date2._d1
}
答案 1 :(得分:1)
在>
运算符重载中,您有
DateTime firstDate = Convert.ToDateTime(date1);
DateTime secondDate = Convert.ToDateTime(date2);
并且Convert.ToDateTime
没有超载会占用您的Date
对象,因此您要调用Convert.ToDateTime(object)
,这需要object
来实施IConvertible。< / p>
您可以实施IConvertible
,或者仅将_d1
值与@ChaosPandion提及进行比较。