using System; using System.Globalization; namespace Neo.Afx.ComponentModel { /// /// Contains object extension methods /// public static partial class Extensions { #region IsNull /// /// Determines whether the specified object is null or DBNull. /// /// The object to be queried. /// /// true if the specified object is null or DBNull; /// false otherwise. /// public static bool IsNull(this object obj) { return Convert.IsDBNull(obj) || obj == null; } #endregion #region ToByte /// /// Gets the byte value of the object; zero if null. /// /// The object to be queried. /// The byte value of the object; zero if null public static byte? ToByte(this object obj) { byte result; return !obj.IsNull() && byte.TryParse(obj.ToString(), out result) && result != 0 ? (byte?)result : null; } #endregion #region ToByteArray /// /// Gets the byte array value of the object; null otherwise. /// /// The object to be queried. /// The byte array value of the object; null otherwise public static byte[] ToByteArray(this object obj) { return !obj.IsNull() ? obj as byte[] : null; } #endregion #region ToInt16 /// /// Gets the short value of the object; zero if null. /// /// The object to be queried. /// The short value of the object; zero if null public static short? ToInt16(this object obj) { short result; return !obj.IsNull() && Int16.TryParse(obj.ToString(), out result) && result != 0 ? (short?)result : null; } #endregion #region ToInt32 /// /// Gets the int value of the object; zero if null. /// /// The object to be queried. /// The int value of the object; zero if null public static int? ToInt32(this object obj) { int result; return !obj.IsNull() && Int32.TryParse(obj.ToString(), out result) && result != 0 ? (int?)result : null; } #endregion #region ToInt64 /// /// Gets the long value of the object; zero if null. /// /// The object to be queried. /// The long value of the object; zero if null public static long? ToInt64(this object obj) { long result; return !obj.IsNull() && Int64.TryParse(obj.ToString(), out result) && result != 0 ? (long?)result : null; } #endregion #region ToDecimal /// /// Gets the decimal value of the object; zero if null. /// /// The object to be queried. /// The decimal value of the object; zero if null public static decimal? ToDecimal(this object obj) { decimal result; return !obj.IsNull() && decimal.TryParse(obj.ToString(), out result) ? (result != 0 ? (decimal?)result : null) : null; } #endregion #region ToDouble /// /// Gets the double value of the object; zero if null. /// /// The object to be queried. /// The double value of the object; zero if null public static double? ToDouble(this object obj) { double result; return !obj.IsNull() && double.TryParse(obj.ToString(), out result) && result != 0 ? (double?)result : null; } #endregion #region ToFloat /// /// Gets the float value of the object; zero if null. /// /// The object to be queried. /// The float value of the object; zero if null public static float? ToFloat(this object obj) { float result; return !obj.IsNull() && float.TryParse(obj.ToString(), out result) && result != 0 ? (float?)result : null; } #endregion #region ToBoolean /// /// Gets the boolean value of the object; zero if null. /// /// The object to be queried. /// The boolean value of the object; zero if null public static bool ToBoolean(this object obj) { if(obj.IsNull()) { return false; } if(obj.GetType() == typeof(bool)) { return (bool)obj; } if(obj.GetType() == typeof(bool?)) { var nullBool = (bool?)obj; return nullBool.Value; } var value = obj.ToString(); bool bit; if(Boolean.TryParse(value, out bit)) { return bit; } int bitValue; return Int32.TryParse(value, out bitValue) && bitValue > 0; } #endregion #region ToDateTime /// /// Gets the DateTime value of the object; zero if null. /// /// The object to be queried. /// The DateTime value of the object; DateTime.MinValue if null public static DateTime? ToDateTime(this object obj) { DateTime result; if(!obj.IsNull() && DateTime.TryParse(obj.ToString(), out result)) { if(result != DateTime.MinValue) { return result; } } return null; } #endregion #region ToDDMMYYYY /// /// Gets the date in "dd-MM-yyyy" format. /// /// The object to be formatted. /// A string representation of the date. public static string ToDDMMYYYY(this object dateObj) { DateTime date; if(!dateObj.IsNull() && DateTime.TryParse(dateObj.ToString(), out date)) { return date.ToString("dd-MM-yyyy"); } return String.Empty; } #endregion #region ToYYYYMMDD /// /// Gets the date in "yyyy-MM-dd" format. /// /// The object to be formatted. /// A string representation of the date. public static string ToYYYYMMDD(this object dateObj) { DateTime date; if(!dateObj.IsNull() && DateTime.TryParse(dateObj.ToString(), out date)) { return date.ToString("yyyy-MM-dd"); } return String.Empty; } #endregion #region ToHHMM /// /// Gets the time in "HH:mm" format. /// /// The object to be formatted. /// A string representation of the date. public static string ToHHMM(this object dateObj) { DateTime date; if(!dateObj.IsNull() && DateTime.TryParse(dateObj.ToString(), out date)) { return date.ToString("HH:mm"); } return String.Empty; } #endregion #region ToHHMMSS /// /// Gets the time in "HH:mm:ss" format. /// /// The object to be formatted. /// A string representation of the date. public static string ToHHMMSS(this object dateObj) { DateTime date; if(!dateObj.IsNull() && DateTime.TryParse(dateObj.ToString(), out date)) { return date.ToString("HH:mm:ss"); } return String.Empty; } #endregion #region ToClr /// /// Converts the object to it's CLR representation (e.g. DBNull to null). /// /// The object to be queried. /// The CLR representation of the object. public static object ToClr(this object obj) { return obj.IsNull() ? null : obj; } #endregion #region ToStringSafe /// /// Converts the object to its string representation; /// returns String.Empty if it is a DBNull /// or a null value. /// /// The object to be queried. /// A string representation of the object. public static string ToStringSafe(this object obj) { return obj.IsNull() ? String.Empty : obj.ToString(); } #endregion #region ToDbValue /// /// Converts the object to Convert.DBNull if it is null. /// /// The object to be queried. /// Convert.DBNull if the object is null. public static object ToDbValue(this object obj) { return obj ?? Convert.DBNull; } /// /// Converts the object to Convert.DBNull if it is null. /// /// The object to be queried. /// Convert.DBNull if the object is null. public static object ToDbValue(this T? obj) where T : struct { return obj.HasValue ? obj.Value : Convert.DBNull; } /// /// Converts the object to Convert.DBNull if it is DateTime.MinValue. /// /// The object to be queried. /// Convert.DBNull if the object is null. public static object ToDbValue(this DateTime obj) { return obj == DateTime.MinValue ? Convert.DBNull : obj; } #endregion #region Value /// /// Gets the value at the given index. /// /// The array to be queried. /// The index of the value to be retrieved. /// /// The value at the given index if found; null otherwise. /// public static object Value(this object[] values, int index) { if(values != null && values.Length > index) { return values[index]; } return null; } #endregion #region ChangeType /// /// Converts the given to an object of the given . /// /// The object to be converted. /// The type of the result. /// An object of the requested type. public static object ChangeType(this object obj, Type type) { if(type.IsSubclassOf(typeof(Enum))) { var s = obj.ToStringSafe(); return s.ToEnum(type); } if(type == typeof(bool)) { return obj.ToBoolean(); } if(type == typeof(DateTime?)) { return obj.ToDateTime(); } if(type == typeof(DateTime)) { return obj.ToDateTime() ?? DateTime.MinValue; } if(type == typeof(byte?)) { return obj.ToByte(); } if(type == typeof(byte)) { return obj.ToByte() ?? 0; } if(type == typeof(short?)) { return obj.ToInt16(); } if(type == typeof(short)) { return obj.ToInt16() ?? 0; } if(type == typeof(int?)) { return obj.ToInt32(); } if(type == typeof(int)) { return obj.ToInt32() ?? 0; } if(type == typeof(long?)) { return obj.ToInt64(); } if(type == typeof(long)) { return obj.ToInt64() ?? 0; } if(type == typeof(float?)) { return obj.ToFloat(); } if(type == typeof(float)) { return obj.ToFloat() ?? 0; } if(type == typeof(double?)) { return obj.ToDouble(); } if(type == typeof(double)) { return obj.ToDouble() ?? 0; } if(type == typeof(decimal?)) { return obj.ToDecimal(); } if(type == typeof(decimal)) { return obj.ToDecimal() ?? 0; } return Convert.ChangeType(obj, type, CultureInfo.InvariantCulture); } /// /// Converts the given to an object of the given . /// /// The object to be converted. /// The type code of the result. /// An object of the requested type. public static object ChangeType(this object obj, string typeCode) { return obj.ChangeType((TypeCode)typeCode.ToEnum(typeof(TypeCode))); } /// /// Converts the given to an object of the given . /// /// The object to be converted. /// The of the result. /// An object of the requested type. public static object ChangeType(this object obj, TypeCode typeCode) { if(typeCode == TypeCode.Boolean) { return obj.ToBoolean(); } return typeCode == TypeCode.DateTime ? obj.ToDateTime() : Convert.ChangeType(obj, typeCode, CultureInfo.InvariantCulture); } #endregion } }