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.
6 using System.Collections;
8 namespace System.Runtime.InteropServices.CustomMarshalers
10 internal class EnumerableToDispatchMarshaler : ICustomMarshaler
12 private static readonly EnumerableToDispatchMarshaler s_enumerableToDispatchMarshaler = new EnumerableToDispatchMarshaler();
14 public static ICustomMarshaler GetInstance(string? cookie) => s_enumerableToDispatchMarshaler;
16 private EnumerableToDispatchMarshaler()
20 public void CleanUpManagedData(object ManagedObj)
24 public void CleanUpNativeData(IntPtr pNativeData)
26 Marshal.Release(pNativeData);
29 public int GetNativeDataSize()
31 // Return -1 to indicate the managed type this marshaler handles is not a value type.
35 public IntPtr MarshalManagedToNative(object ManagedObj)
37 if (ManagedObj == null)
39 throw new ArgumentNullException(nameof(ManagedObj));
42 return Marshal.GetComInterfaceForObject<object, IEnumerable>(ManagedObj);
45 public object MarshalNativeToManaged(IntPtr pNativeData)
47 if (pNativeData == IntPtr.Zero)
49 throw new ArgumentNullException(nameof(pNativeData));
52 object comObject = Marshal.GetObjectForIUnknown(pNativeData);
54 return ComDataHelpers.GetOrCreateManagedViewFromComData<object, EnumerableViewOfDispatch>(comObject, obj => new EnumerableViewOfDispatch(obj));