public static T Value <T>(this object objValue, T defaultValue) { if (objValue == null || objValue == DBNull.Value) { return(defaultValue); } Type destType = typeof(T); if (objValue.GetType() == destType) { return((T)objValue); } try { return((T)System.Convert.ChangeType(objValue, destType, ObjectUtil.SysCulture)); } catch { return(ObjectUtil.GetDefaultValue <T>(objValue.ToString(), defaultValue)); } }
public static object Value(this object objValue, Type type) { TkDebug.AssertArgumentNull(type, "type", null); if (objValue == null || objValue == DBNull.Value) { return(ObjectUtil.GetTypeDefaultValue(type)); } if (objValue.GetType() == type) { return(objValue); } try { return(System.Convert.ChangeType(objValue, type, ObjectUtil.SysCulture)); } catch { return(ObjectUtil.GetDefaultValue(objValue.ToString(), type)); } }