Exemple #1
0
        /// <summary>
        /// Attempts to case compareToThis as an DateTime
        /// to do the comparison. If the cast
        /// fails, then it compares the current
        /// value against DateTime.MinValue.
        /// </summary>
        /// <param name="compareToThis"></param>
        /// <returns></returns>
        public int CompareTo(object compareToThis)
        {
            System.Type type = compareToThis.GetType();

            DateTime compareTo = DateTime.MinValue;

            try
            {
                if (type == typeof(PropertyDateTime))
                {
                    compareTo = ((PropertyDateTime)compareToThis).m_value;
                }
                else if (type == typeof(string))
                {
                    compareTo = PropertyDateTime.ParseIso8601((string)compareToThis);
                }
                else
                {
                    compareTo = (DateTime)compareToThis;
                }
            }
            catch
            {
                compareTo = DateTime.MinValue;
            }

            return(this.m_value.CompareTo(compareTo));
        }
        /// <summary>
        /// This method is useful if metadata values are already obtained and a
        /// comparison is needed. The method compares val1 against val2.
        /// If the types don't match, then the type of val1 is
        /// used to determine the semantics of val2, and an attempt
        /// to cast val2 into the same type of val1 is made. This is useful
        /// when val1 is a uri/number and val2 is a string.
        /// </summary>
        /// <param name="val1">a value of some type</param>
        /// <param name="val2">another value of the same type, or a string that can be cast into the type of val1</param>
        /// <param name="ignoreCase">indicates if we should ignore the case for text-related comparisons</param>
        /// <returns>a comparison result: 0, 1, -1</returns>
        public static int CompareTagValues(object val1, object val2, bool ignoreCase)
        {
            string type1 = val1.GetType().ToString();
            string type2 = val2.GetType().ToString();

            object v1 = val1;
            object v2 = val2;

            if (type1 != type2)
            {
                // If the data types for the two objects don't match
                // I do my best to convert the second object
                // into a data type of the first object.
                string strval2 = val2.ToString();
                switch (type1)
                {
                case "System.Char":
                    v2 = strval2[0];
                    break;

                case "System.String":
                    v2 = strval2;
                    break;

                case "System.Boolean":
                    if ((strval2 == "0") || (strval2 == "no"))
                    {
                        v2 = false;
                    }
                    else if ((strval2 == "1") || (strval2 == "yes"))
                    {
                        v2 = true;
                    }
                    else
                    {
                        try
                        {
                            v2 = bool.Parse(strval2);
                        }
                        catch
                        {
                            v2 = strval2;
                        }
                    }
                    break;

                case "System.Uri":
                    //Uri is an exception - we'll always compare as a string.
                    v2 = strval2;
                    break;

                case "System.Byte":
                    v2 = byte.Parse(strval2);
                    break;

                case "System.UInt16":
                    v2 = UInt16.Parse(strval2);
                    break;

                case "System.UInt32":
                    v2 = UInt32.Parse(strval2);
                    break;

                case "System.Int32":
                    v2 = Int32.Parse(strval2);
                    break;

                case "System.Int16":
                    v2 = Int16.Parse(strval2);
                    break;

                case "System.SByte":
                    v2 = sbyte.Parse(strval2);
                    break;

                case "System.Single":
                    v2 = Single.Parse(strval2);
                    break;

                case "System.Double":
                    v2 = Double.Parse(strval2);
                    break;

                case "System.DateTime":
                    DateTimeFormatInfo formatInfo = PropertyDateTime.GetFormatInfo();
                    v2 = DateTime.Parse(strval2, formatInfo);
                    break;

                default:
                    throw new Exception("Cannot recast object of " + type2 + " into object of " + type1);
                }
            }

            return(CompareObject(v1, v2, ignoreCase));
        }