Skip to content

Commit 752809a

Browse files
committed
Use INumberBase in .NET 8 build
1 parent 9e14d36 commit 752809a

File tree

1 file changed

+42
-3
lines changed

1 file changed

+42
-3
lines changed

DuckDB.NET.Data/Internal/Reader/NumericVectorDataReader.cs

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,21 +242,60 @@ private static T CastTo<T>(BigInteger bigInteger)
242242
}
243243

244244
private TResult GetUnmanagedTypeValue<TQuery, TResult>(ulong offset) where TQuery : unmanaged
245+
#if NET8_0_OR_GREATER
246+
, INumberBase<TQuery>
247+
#endif
245248
{
249+
var resultType = typeof(TResult);
246250
var value = GetFieldData<TQuery>(offset);
247251

248-
if (typeof(TQuery) == typeof(TResult))
252+
if (typeof(TQuery) == resultType)
249253
{
250254
return Unsafe.As<TQuery, TResult>(ref value);
251255
}
252256

253257
try
254258
{
255-
return (TResult)Convert.ChangeType(value, typeof(TResult));
259+
#if NET8_0_OR_GREATER
260+
if (resultType == typeof(byte))
261+
{
262+
return (TResult)(object)byte.CreateChecked(value);
263+
}
264+
if (resultType == typeof(sbyte))
265+
{
266+
return (TResult)(object)sbyte.CreateChecked(value);
267+
}
268+
if (resultType == typeof(short))
269+
{
270+
return (TResult)(object)short.CreateChecked(value);
271+
}
272+
if (resultType == typeof(ushort))
273+
{
274+
return (TResult)(object)ushort.CreateChecked(value);
275+
}
276+
if (resultType == typeof(int))
277+
{
278+
return (TResult)(object)int.CreateChecked(value);
279+
}
280+
if (resultType == typeof(uint))
281+
{
282+
return (TResult)(object)uint.CreateChecked(value);
283+
}
284+
if (resultType == typeof(long))
285+
{
286+
return (TResult)(object)long.CreateChecked(value);
287+
}
288+
if (resultType == typeof(ulong))
289+
{
290+
return (TResult)(object)ulong.CreateChecked(value);
291+
}
292+
#endif
293+
294+
return (TResult)Convert.ChangeType(value, resultType);
256295
}
257296
catch (OverflowException)
258297
{
259-
throw new InvalidCastException($"Cannot cast from {value.GetType().Name} to {typeof(TResult).Name} in column {ColumnName}");
298+
throw new InvalidCastException($"Cannot cast from {value.GetType().Name} to {resultType.Name} in column {ColumnName}");
260299
}
261300
}
262301
}

0 commit comments

Comments
 (0)