public virtual object Cast(Type type, object obj) { object result; if (type.IsArray) { result = obj; } else if (type.IsEnum) { result = InnerConvert.ToInt32(obj); } else if (type.Equals(typeof(short)) || type.Equals(typeof(byte)) || type.Equals(typeof(byte)) || type.Equals(typeof(short))) { result = InnerConvert.ToInt16(obj); } else if (type.Equals(typeof(long)) || type.Equals(typeof(long))) { result = InnerConvert.ToInt64(obj); } else if (type.Equals(typeof(int)) || type.Equals(typeof(int))) { result = InnerConvert.ToInt32(obj); } else if (type.Equals(typeof(double)) || type.Equals(typeof(float)) || type.Equals(typeof(double))) { result = InnerConvert.ToDouble(obj); } else if (type.Equals(typeof(decimal))) { result = InnerConvert.ToDecimal(obj); } else { if (type.Equals(typeof(bool))) { obj = InnerConvert.ToBoolean(obj); } else if (type.Equals(typeof(DateTime))) { obj = InnerConvert.ToDateTime(obj); } else if (obj != null && !type.Equals(typeof(byte[])) && obj.GetType().Equals(typeof(byte[]))) { IFormatter formatter = new BinaryFormatter(); MemoryStream memoryStream = new MemoryStream(obj as byte[]); obj = formatter.Deserialize(memoryStream); memoryStream.Close(); } result = obj; } return(result); }
public static double ToDouble(object obj) { return(InnerConvert.ToDouble(obj, 0.0)); }