From 4061e9de5a7988daef8f7bdf89a12a927bf32cd3 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 netcore/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs diff --git a/netcore/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems b/netcore/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems index 90d68d4156f2..5e8a46b1375e 100644 --- a/netcore/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems +++ b/netcore/System.Private.CoreLib/shared/System.Private.CoreLib.Shared.projitems @@ -644,6 +644,7 @@ + diff --git a/netcore/System.Private.CoreLib/shared/System/Collections/Generic/List.cs b/netcore/System.Private.CoreLib/shared/System/Collections/Generic/List.cs index 62993e71e555..ce372c255676 100644 --- a/netcore/System.Private.CoreLib/shared/System/Collections/Generic/List.cs +++ b/netcore/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/netcore/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs b/netcore/System.Private.CoreLib/shared/System/Runtime/InteropServices/CollectionsMarshal.cs new file mode 100644 index 000000000000..fe9df260ac6b --- /dev/null +++ b/netcore/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); + } +}