private static object ConvertIfNecessary(object value, Type objectType, ObjectTypeCode typeCode, ObjectTypeCode expectedTypeCode, bool isConvertible) { if (expectedTypeCode != typeCode && isConvertible) { return(Reflector.ChangeType(value, objectType)); } return(value); }
public static T SafeCast <T>(this object source) { var destinationType = typeof(T); //if (null == source && destinationType.IsPrimitive) //{ // throw new InvalidCastException(); //} if (null == source || !(source is IConvertible)) { return((T)source); } return((T)Reflector.ChangeType(source, destinationType)); }
public static async Task <T> RetrieveScalarAsync <T>(string sql, Param[] parameters = null, string connectionName = null, DbConnection connection = null, string schema = null) where T : struct { var response = connection != null ? await ExecuteAsync(sql, parameters, OperationReturnType.Scalar, OperationType.Sql, connection : connection, schema : schema) : await ExecuteAsync(sql, parameters, OperationReturnType.Scalar, OperationType.Sql, connectionName : connectionName, schema : schema); var value = response.Value; if (value == null) { return(default(T)); } return((T)Reflector.ChangeType(value, typeof(T))); }