public void GetDefaultEmptyTest() { object emptyValue; ConvertEx.ResetEmptyValues(); emptyValue = ConvertEx.GetDefaultEmptyValue <object>(); Assert.IsNull(emptyValue); emptyValue = ConvertEx.GetDefaultEmptyValue <string>(); Assert.IsNull(emptyValue, string.Format("The value is '{0}'.", emptyValue)); emptyValue = ConvertEx.GetDefaultEmptyValue <int>(); Assert.AreEqual(int.MinValue, emptyValue); emptyValue = ConvertEx.GetDefaultEmptyValue <char>(); Assert.AreEqual('\0', emptyValue); emptyValue = ConvertEx.GetDefaultEmptyValue <DateTime>(); Assert.AreEqual(DateTime.MinValue, emptyValue); emptyValue = ConvertEx.GetDefaultEmptyValue <ConvertTest>(); Assert.IsNull(emptyValue); emptyValue = ConvertEx.GetDefaultEmptyValue <ConvertStructTest>(); Assert.AreEqual(ConvertStructTest.Empty, emptyValue); }
/// <summary> /// Creates an instance of BaField /// </summary> /// <param name="obj">The object this belongs to.</param> /// <param name="name">Name of the field.</param> /// <param name="fieldType">The data type for the field.</param> /// <param name="dflt">Default value for the field. Used to determine if the field has changed. If null, it is converted to the default value for fieldType.</param> internal BaField(BaObject obj, string name, Type fieldType, object dflt = null) { if (obj == null) { throw new ArgumentNullException(nameof(obj)); } if (name.IsEmpty()) { throw new ArgumentNullException(nameof(name)); } if (fieldType == null) { throw new ArgumentNullException(nameof(fieldType)); } Object = obj; Name = name; FieldType = fieldType; if (dflt == null || dflt == DBNull.Value) { mDefaultValue = ConvertEx.GetDefaultEmptyValue(fieldType); } else { VerifyValue(dflt, "default value"); mDefaultValue = dflt; } }
/// <summary> /// Changes the type of the value. /// </summary> /// <param name="from"></param> /// <param name="to"></param> /// <param name="value"></param> /// <param name="provider"></param> /// <returns></returns> public object Convert(Type from, Type to, object value, IFormatProvider provider) { if (to == null) { return(null); } return(ConvertEx.GetDefaultEmptyValue(to)); }
/// <summary> /// Changes the type of the value. /// </summary> /// <param name="value">The object to convert.</param> /// <param name="to">The type to convert the value to.</param> /// <param name="convertedValue">Return the value if converted.</param> /// <returns>True if able to convert the value.</returns> public bool TryConvert(object value, Type to, out object convertedValue) { if (value == null) { convertedValue = ConvertEx.GetDefaultEmptyValue(to); return(true); } else { convertedValue = null; return(false); } }
/// <summary> /// Executes the command and returns the first column of the first row. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="cmd"></param> /// <returns></returns> public T ExecuteScalar <T>(DbCommand cmd) { var dflt = (T)ConvertEx.GetDefaultEmptyValue(typeof(T)); return(ExecuteScalar <T>(cmd, dflt)); }
/// <summary> /// Gets an integer from an attribute or node. /// </summary> /// <param name="node"></param> /// <param name="name"></param> /// <returns></returns> public static int GetInt(this XmlNode node, string name) { var dfltVal = ConvertEx.GetDefaultEmptyValue <int>(); return(GetInt(node, name)); }
/// <summary> /// Gets a string from an attribute or node. /// </summary> /// <param name="node"></param> /// <param name="name"></param> /// <returns></returns> public static string GetString(this XmlNode node, string name) { var dfltVal = ConvertEx.GetDefaultEmptyValue <string>(); return(GetString(node, name, dfltVal)); }