无法强制转换类型为';通用。日期';输入';System.IConvertible C#

无法强制转换类型为';通用。日期';输入';System.IConvertible C#,c#,asp.net,operator-overloading,C#,Asp.net,Operator Overloading,我在stackoverflow中看到了这个错误消息问题,但它们都不是针对datetime或date类型的。为了只使用date类型,我创建了一个date类型的类,并在date类中为其编写了一些重载。我的约会课是 using System; namespace Common { public class Date { private DateTime _d1; public Date(DateTime dateTime) {

我在stackoverflow中看到了这个错误消息问题,但它们都不是针对datetime或date类型的。为了只使用date类型,我创建了一个date类型的类,并在date类中为其编写了一些重载。我的约会课是

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
在item对象中,purchasedate和Submission date是datetime属性,错误在if行
有人能给我一些建议吗?此问题的可能解决方案是什么?

您可以直接访问
日期
的字段。尽管我质疑这个
日期
对象的有用性

public static bool operator <(Date date1, Date date2)
{
    return date1 != null && date2 != null && date1._d1 < date2._d1
}

publicstaticbool操作符您可以直接访问
Date
的字段。尽管我质疑这个
日期
对象的有用性

public static bool operator <(Date date1, Date date2)
{
    return date1 != null && date2 != null && date1._d1 < date2._d1
}

公共静态布尔运算符在您的
运算符重载中,您有

DateTime firstDate = Convert.ToDateTime(date1); 
DateTime secondDate = Convert.ToDateTime(date2);
而且没有使用
Date
对象的
Convert.ToDateTime
重载,因此您正在调用,这需要
object
来实现


您可以实现
IConvertible
,或者只需比较@chaospanion提到的
\u d1
值。

运算符重载中,您有

DateTime firstDate = Convert.ToDateTime(date1); 
DateTime secondDate = Convert.ToDateTime(date2);
而且没有使用
Date
对象的
Convert.ToDateTime
重载,因此您正在调用,这需要
object
来实现


您可以实现
IConvertible
,或者只是比较@chaospanion提到的
\u d1
值。

实际上,我编写date类是为了学习c中的运算符重载,然后学习如何使用它们,你确定--date1.\u d1Date
范围内访问。实际上,我编写Date类是为了学习c中的运算符重载,然后学习如何使用它们,r你确定--date1.\u d1Date
范围内访问。无法正确理解,请你进一步解释一下好吗?您太好了。如果您查看的重载,您会发现唯一可以调用的是
Convert.ToDateTime(object)
,为了使
Convert.ToDateTime
工作,
object
必须实现
IConvertible
接口(请参阅文档)。由于您的
Date
类未实现
IConvertible
Convert.ToDateTime(date1)
引发异常。无法正确理解,请进一步解释?您太好了。如果您查看的重载,您会发现唯一可以调用的是
Convert.ToDateTime(object)
,为了使
Convert.ToDateTime
工作,
object
必须实现
IConvertible
接口(请参阅文档)。由于您的
Date
类未实现
IConvertible
Convert.ToDateTime(date1)
引发异常。