From 05bbf7dacd2ecb116db664bc638a779a69da0240 Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Wed, 25 Sep 2019 01:24:01 +0100 Subject: [PATCH] Add CollectionsMarshal (dotnet/coreclr#26867) * Add CollectionsMarshal * Feedback * Feedback Signed-off-by: dotnet-bot --- .../System.Private.CoreLib.Shared.projitems | 1 + .../shared/System/Collections/Generic/List.cs | 4 ++-- .../InteropServices/CollectionsMarshal.cs | 21 +++++++++++++++++++ 3 files changed, 24 insertions(+), 2 deletions(-) create mode 100644 src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs diff --git a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 90d68d4156f..5e8a46b1375 100644 --- a/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/src/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -644,6 +644,7 @@ + diff --git a/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs b/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs index 62993e71e55..ce372c25567 100644 --- a/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs +++ b/src/System.Private.CoreLib/shared/System/Collections/Generic/List.cs @@ -23,8 +23,8 @@ public class List : IList, IList, IReadOnlyList { private const int DefaultCapacity = 4; - private T[] _items; // Do not rename (binary serialization) - private int _size; // Do not rename (binary serialization) + internal T[] _items; // Do not rename (binary serialization) + internal int _size; // Do not rename (binary serialization) private int _version; // Do not rename (binary serialization) #pragma warning disable CA1825 // avoid the extra generic instantiation for Array.Empty() diff --git a/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs new file mode 100644 index 00000000000..fe9df260ac6 --- /dev/null +++ b/src/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs @@ -0,0 +1,21 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. +// See the LICENSE file in the project root for more information. + +using System.Collections.Generic; + +namespace System.Runtime.InteropServices +{ + /// + /// An unsafe class that provides a set of methods to access the underlying data representations of collections. + /// + public static class CollectionsMarshal + { + /// + /// Get a view over a 's data. + /// Items should not be added or removed from the while the is in use. + /// + public static Span AsSpan(List list) + => new Span(list._items, 0, list._size); + } +}