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.
5 using System.Collections;
7 namespace System.Runtime.InteropServices.CustomMarshalers
9 internal class EnumerableToDispatchMarshaler : ICustomMarshaler
11 private static readonly EnumerableToDispatchMarshaler s_enumerableToDispatchMarshaler = new EnumerableToDispatchMarshaler();
13 public static ICustomMarshaler GetInstance(string cookie) => s_enumerableToDispatchMarshaler;
15 private EnumerableToDispatchMarshaler()
19 public void CleanUpManagedData(object ManagedObj)
23 public void CleanUpNativeData(IntPtr pNativeData)
25 Marshal.Release(pNativeData);
28 public int GetNativeDataSize()
30 // Return -1 to indicate the managed type this marshaler handles is not a value type.
34 public IntPtr MarshalManagedToNative(object ManagedObj)
36 if (ManagedObj == null)
38 throw new ArgumentNullException(nameof(ManagedObj));
41 return Marshal.GetComInterfaceForObject<object, IEnumerable>(ManagedObj);
44 public object MarshalNativeToManaged(IntPtr pNativeData)
46 if (pNativeData == IntPtr.Zero)
48 throw new ArgumentNullException(nameof(pNativeData));
51 object comObject = Marshal.GetObjectForIUnknown(pNativeData);
53 return ComDataHelpers.GetOrCreateManagedViewFromComData<object, EnumerableViewOfDispatch>(comObject, obj => new EnumerableViewOfDispatch(obj));