繁体   English   中英

无法将类型为“ Common.Date”的对象转换为类型为“ System.IConvertible C#”

[英]Unable to cast object of type 'Common.Date' to type 'System.IConvertible C#

我已经在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

但是问题是当我试图在页面后面的代码中使用它时,会给我这个错误-无法将类型为'Common.Date'的对象转换为类型为'System.IConvertible

我在其中使用它的代码,例如Date PurchaseDate = new Date(item.PurchaseDate); 日期SubmitSate =新的Date(item.SubmissionDate);

                    if (purchaseDate>submissionSate)
                    {
                        //to do
                    }

这在item对象中,purchateate和submision日期是datetime属性,并且错误在if行中有人可以提供我任何建议吗? 这个问题的可能解决方案是什么?

您可以直接访问Date字段。 尽管我对此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重载,因此您正在调用Convert.ToDateTime(object) ,这需要object实现IConvertible

您可以实现IConvertible ,或者只比较@ChaosPandion提到的_d1值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM