Nullable: System.Runtime.InteropServices.CustomMarshalers/WindowsRuntime (#23930)
[platform/upstream/coreclr.git] / src / System.Private.CoreLib / src / System / Runtime / InteropServices / CustomMarshalers / EnumerableToDispatchMarshaler.cs
1 // Licensed to the .NET Foundation under one or more agreements.
2 // The .NET Foundation licenses this file to you under the MIT license.
3 // See the LICENSE file in the project root for more information.
4
5 #nullable enable
6 using System.Collections;
7
8 namespace System.Runtime.InteropServices.CustomMarshalers
9 {
10     internal class EnumerableToDispatchMarshaler : ICustomMarshaler
11     {
12         private static readonly EnumerableToDispatchMarshaler s_enumerableToDispatchMarshaler = new EnumerableToDispatchMarshaler();
13
14         public static ICustomMarshaler GetInstance(string? cookie) => s_enumerableToDispatchMarshaler;
15
16         private EnumerableToDispatchMarshaler()
17         {
18         }
19
20         public void CleanUpManagedData(object ManagedObj)
21         {
22         }
23
24         public void CleanUpNativeData(IntPtr pNativeData)
25         {
26             Marshal.Release(pNativeData);
27         }
28
29         public int GetNativeDataSize()
30         {
31             // Return -1 to indicate the managed type this marshaler handles is not a value type.
32             return -1;
33         }
34
35         public IntPtr MarshalManagedToNative(object ManagedObj)
36         {
37             if (ManagedObj == null)
38             {
39                 throw new ArgumentNullException(nameof(ManagedObj));
40             }
41
42             return Marshal.GetComInterfaceForObject<object, IEnumerable>(ManagedObj);
43         }
44
45         public object MarshalNativeToManaged(IntPtr pNativeData)
46         {
47             if (pNativeData == IntPtr.Zero)
48             {
49                 throw new ArgumentNullException(nameof(pNativeData));
50             }
51
52             object comObject = Marshal.GetObjectForIUnknown(pNativeData);
53
54             return ComDataHelpers.GetOrCreateManagedViewFromComData<object, EnumerableViewOfDispatch>(comObject, obj => new EnumerableViewOfDispatch(obj));
55         }
56     }
57 }