private static Array GetArray(Array source, Type childTargetType, IColumnInfo columnInfo) { // Handle struct type cases manually to prevent unboxing and boxing again if (childTargetType == typeof(TimeUuid)) { var arrSource = (Guid[])source; var result = new TimeUuid[source.Length]; for (var i = 0; i < arrSource.Length; i++) { result[i] = arrSource[i]; } return(result); } if (childTargetType == typeof(DateTime)) { var arrSource = (DateTimeOffset[])source; var result = new DateTime[source.Length]; for (var i = 0; i < arrSource.Length; i++) { result[i] = arrSource[i].DateTime; } return(result); } // Other collections var childColumnInfo = ((ICollectionColumnInfo)columnInfo).GetChildType(); var arr = Array.CreateInstance(childTargetType, source.Length); for (var i = 0; i < source.Length; i++) { arr.SetValue(TryConvertToType(source.GetValue(i), childColumnInfo, childTargetType), i); } return(arr); }
private static Array GetArray(Array source, Type childTargetType, IColumnInfo columnInfo) { // Handle struct type cases manually to prevent unboxing and boxing again if (childTargetType.GetTypeInfo().IsAssignableFrom(typeof(TimeUuid))) { var arrSource = (Guid[])source; var result = new TimeUuid[source.Length]; for (var i = 0; i < arrSource.Length; i++) { result[i] = arrSource[i]; } return(result); } if (childTargetType.GetTypeInfo().IsAssignableFrom(typeof(DateTime))) { var arrSource = (DateTimeOffset[])source; var result = new DateTime[source.Length]; for (var i = 0; i < arrSource.Length; i++) { result[i] = arrSource[i].DateTime; } return(result); } // Other collections var childColumnInfo = ((ICollectionColumnInfo)columnInfo).GetChildType(); var arr = Array.CreateInstance(childTargetType, source.Length); bool?isNullable = null; for (var i = 0; i < source.Length; i++) { var value = source.GetValue(i); if (value == null) { if (isNullable == null) { isNullable = !childTargetType.GetTypeInfo().IsValueType; } if (!isNullable.Value) { var nullableType = typeof(Nullable <>).MakeGenericType(childTargetType); var newResult = Array.CreateInstance(nullableType, source.Length); for (var j = 0; j < i; j++) { newResult.SetValue(arr.GetValue(j), j); } arr = newResult; childTargetType = nullableType; isNullable = true; } } arr.SetValue(TryConvertToType(source.GetValue(i), childColumnInfo, childTargetType), i); } return(arr); }