private static object TryConvertToCollection(object value, ColumnDesc column, Type targetType) { var targetTypeInfo = targetType.GetTypeInfo(); // value is an array, according to TypeCodec var childType = value.GetType().GetTypeInfo().GetElementType(); Type childTargetType; if (targetTypeInfo.IsArray) { childTargetType = targetTypeInfo.GetElementType(); if (childTargetType.GetTypeInfo().IsAssignableFrom(childType)) { return(value); } return(GetArray((Array)value, childTargetType, column.TypeInfo)); } if (Utils.IsIEnumerable(targetType, out childTargetType)) { var genericTargetType = targetType.GetGenericTypeDefinition(); // Is IEnumerable if (!childTargetType.GetTypeInfo().IsAssignableFrom(childType)) { // Conversion is needed value = GetArray((Array)value, childTargetType, column.TypeInfo); } if (genericTargetType == typeof(IEnumerable <>)) { // The target type is an interface return(value); } if (column.TypeCode == ColumnTypeCode.List || genericTargetType == typeof(List <>) || genericTargetType == typeof(IList <>)) { // Use List<T> by default when a list is expected and the target type // is not an object or an array return(Utils.ToCollectionType(typeof(List <>), childTargetType, (Array)value)); } if (genericTargetType == typeof(SortedSet <>) || genericTargetType == typeof(ISet <>)) { return(Utils.ToCollectionType(typeof(SortedSet <>), childTargetType, (Array)value)); } if (genericTargetType == typeof(HashSet <>)) { return(Utils.ToCollectionType(typeof(HashSet <>), childTargetType, (Array)value)); } } throw new InvalidCastException(string.Format("Unable to cast object of type '{0}' to type '{1}'", value.GetType(), targetType)); }
/// <summary> /// Handle conversions for some types that, for backward compatibility, /// the result type can be more than 1 depending on the type provided by the user /// </summary> internal static object TryConvertToType(object value, ColumnDesc column, Type targetType) { if (value == null) { return(null); } switch (column.TypeCode) { case ColumnTypeCode.List: { //value is an array, according to TypeCodec if (targetType.IsArray || targetType == typeof(object) || Utils.IsIEnumerable(targetType)) { //Return the underlying array return(value); } var childTypeInfo = (ListColumnInfo)column.TypeInfo; return(Utils.ToCollectionType(typeof(List <>), TypeCodec.GetDefaultTypeFromCqlType(childTypeInfo.ValueTypeCode, childTypeInfo.ValueTypeInfo), (Array)value)); } case ColumnTypeCode.Set: { //value is an array, according to TypeCodec if (targetType.IsArray || targetType == typeof(object) || Utils.IsIEnumerable(targetType)) { //Return the underlying array return(value); } var childTypeInfo = (SetColumnInfo)column.TypeInfo; var itemType = TypeCodec.GetDefaultTypeFromCqlType(childTypeInfo.KeyTypeCode, childTypeInfo.KeyTypeInfo); if (targetType.IsGenericType) { var genericType = targetType.GetGenericTypeDefinition(); if (genericType == typeof(SortedSet <>)) { return(Utils.ToCollectionType(typeof(SortedSet <>), itemType, (Array)value)); } if (genericType == typeof(HashSet <>)) { return(Utils.ToCollectionType(typeof(HashSet <>), itemType, (Array)value)); } } return(Utils.ToCollectionType(typeof(List <>), itemType, (Array)value)); } case ColumnTypeCode.Timestamp: //value is a DateTimeOffset if (targetType == typeof(object) || targetType == typeof(DateTimeOffset)) { return(value); } return(((DateTimeOffset)value).DateTime); case ColumnTypeCode.Timeuuid: //Value is a Uuid if (targetType == typeof(TimeUuid)) { return((TimeUuid)(Guid)value); } return(value); default: return(value); } }
/// <summary> /// Handle conversions for some types that, for backward compatibility, /// the result type can be more than 1 depending on the type provided by the user /// </summary> internal static object TryConvertToType(object value, CqlColumn column, Type targetType) { if (value == null) { return(null); } switch (column.TypeCode) { case ColumnTypeCode.List: { //value is an array, according to TypeCodec if (targetType.IsArray || targetType == typeof(object) || Utils.IsIEnumerable(targetType)) { //Return the underlying array return(value); } return(Utils.ToCollectionType(typeof(List <>), column.Type.GetTypeInfo().GetGenericArguments()[0], (Array)value)); } case ColumnTypeCode.Set: { //value is an array, according to TypeCodec if (targetType.IsArray || targetType == typeof(object) || Utils.IsIEnumerable(targetType)) { //Return the underlying array return(value); } var itemType = column.Type.GetTypeInfo().GetGenericArguments()[0]; if (targetType.GetTypeInfo().IsGenericType) { var genericType = targetType.GetGenericTypeDefinition(); if (genericType == typeof(SortedSet <>)) { return(Utils.ToCollectionType(typeof(SortedSet <>), itemType, (Array)value)); } if (genericType == typeof(HashSet <>)) { return(Utils.ToCollectionType(typeof(HashSet <>), itemType, (Array)value)); } } return(Utils.ToCollectionType(typeof(List <>), itemType, (Array)value)); } case ColumnTypeCode.Timestamp: //value is a DateTimeOffset if (targetType == typeof(object) || targetType == typeof(DateTimeOffset)) { return(value); } return(((DateTimeOffset)value).DateTime); case ColumnTypeCode.Timeuuid: //Value is a Uuid if (targetType == typeof(TimeUuid) && !(value is TimeUuid)) { return((TimeUuid)(Guid)value); } return(value); default: return(value); } }