Exemple #1
0
        /// <summary>
        /// The is Cast on steroids
        /// </summary>
        /// <param name="type">The type to cast to</param>
        /// <param name="value">The value to be cast</param>
        /// <returns></returns>
        public static object The(object type, object value)
        {
            if (type == typeof(Cons))
            {
                // String to List
                if (value is System.String)
                {
                    return(Cons.FromString(value as string));
                }
                // Array to List
                if (value is IList)
                {
                    return(Cons.FromList((IList)value));
                }
                // Hashtable to List
                if (value is IDictionary)
                {
                    return(Cons.FromDictionary((IDictionary)value));
                }
                // Collection to List
                if (value is ICollection)
                {
                    return(Cons.FromICollection((ICollection)value));
                }
            }
            else
            {
            }
            // List to String
            if ((type == typeof(System.String)) && (Primitives.IsList(value)))
            {
                return(ConsToString(value));
            }

            // List to Array
            if ((type == typeof(Array)) && (value is Cons))
            {
                return(((Cons)value).ToArray());
            }

            // List to Hashtable
            if ((type == typeof(Hashtable)) && (Primitives.IsList(value)))
            {
                return(Conversions.ConsToHashtable(value));
            }


#if SYSTEM_DATA
            // DataTable to List
            if ((type == typeof(LSharp.Cons)) && (value is System.Data.DataTable))
            {
                return(Cons.FromDataTable((System.Data.DataTable)value));
            }

            // DataRow to List
            if ((type == typeof(LSharp.Cons) && (value is DataRow)))
            {
                return(Cons.FromDataRow((DataRow)value));
            }
#endif

            // List to Stack
            if ((type == typeof(Stack)) && (Primitives.IsList(value)))
            {
                return(Conversions.ConsToStack(value));
            }

            // List to Queue
            if ((type == typeof(Queue)) && (Primitives.IsList(value)))
            {
                return(ConsToQueue(value));
            }

            // List to SortedList
            if ((type == typeof(SortedList)) && (Primitives.IsList(value)))
            {
                return(Conversions.ConsToSortedList(value));
            }

            // Anything to String
            if (type == typeof(System.String))
            {
                return(value.ToString());
            }

            // Anything to Boolean true or false
            if (type == typeof(System.Boolean))
            {
                return(ObjectToBoolean(value));
            }

            // Allow upcasting for types eg (the Type (typeof string))
            if (((Type)type).IsInstanceOfType(value))
            {
                return(value);
            }

            // Revert to standard type cast
            return(Convert.ChangeType(value, (Type)type));
        }