You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

RuntimeHelpers.cs 1.2 kB

123456789101112131415161718192021222324252627282930313233343536373839
  1. namespace System.Runtime.CompilerServices
  2. {
  3. internal static class RuntimeHelpers
  4. {
  5. /// <summary>
  6. /// Slices the specified array using the specified range.
  7. /// </summary>
  8. public static T[] GetSubArray<T>(T[] array, Range range)
  9. {
  10. if (array == null)
  11. {
  12. throw new ArgumentNullException(nameof(array));
  13. }
  14. (int offset, int length) = range.GetOffsetAndLength(array.Length);
  15. if (default(T) != null || typeof(T[]) == array.GetType())
  16. {
  17. // We know the type of the array to be exactly T[].
  18. if (length == 0)
  19. {
  20. return Array.Empty<T>();
  21. }
  22. var dest = new T[length];
  23. Array.Copy(array, offset, dest, 0, length);
  24. return dest;
  25. }
  26. else
  27. {
  28. // The array is actually a U[] where U:T.
  29. var dest = (T[])Array.CreateInstance(array.GetType().GetElementType(), length);
  30. Array.Copy(array, offset, dest, 0, length);
  31. return dest;
  32. }
  33. }
  34. }
  35. }