我已经看到了此错误消息问题在计算器中,但它们都不是日期时间或日期类型为了使用只有日期类型I已经创建了一类日期类型,并在日期类中为它写了一些重载。我的约会类是无法投入'Common.Date'类型的对象来键入'System.IConvertible C#
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); date submissionSate = new Date(item.SubmissionDate);
if (purchaseDate>submissionSate)
{
//to do
}
在这里的项目对象而purchaseDate和submision日期是日期时间属性和错误的,如果行 谁能给我提供任何suggesion?这个问题的可能解决方案是什么?
无法正确理解,请您进一步解释一下?这对你真好。 – Pankouri
如果您查看['Convert.ToDateTime']的重载(http://msdn.microsoft.com/zh-cn/library/dw6yd06a.aspx),您会看到唯一一个可以是被称为'Convert.ToDateTime(object)',并且为了使Convert.ToDateTime工作,'object'必须实现'IConvertible'接口(参见文档)。由于你的Date类没有实现'IConvertible','Convert.ToDateTime(date1)'抛出异常。 –
感谢问题解决。 – Pankouri