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;
7 using System.Runtime.InteropServices.ComTypes;
9 namespace System.Runtime.InteropServices.CustomMarshalers
11 internal class EnumerableViewOfDispatch : ICustomAdapter, System.Collections.IEnumerable
13 // Reserved DISPID slot for getting an enumerator from an IDispatch-implementing COM interface.
14 private const int DISPID_NEWENUM = -4;
15 private const int LCID_DEFAULT = 1;
16 private readonly object _dispatch;
18 public EnumerableViewOfDispatch(object dispatch)
23 private IDispatch Dispatch => (IDispatch)_dispatch;
25 public IEnumerator GetEnumerator()
30 void *resultLocal = &result;
31 DISPPARAMS dispParams = new DISPPARAMS();
32 Guid guid = Guid.Empty;
37 InvokeFlags.DISPATCH_METHOD | InvokeFlags.DISPATCH_PROPERTYGET,
39 new IntPtr(resultLocal),
44 object? resultAsObject = result.ToObject();
45 if (!(resultAsObject is IEnumVARIANT enumVariant))
47 throw new InvalidOperationException(SR.InvalidOp_InvalidNewEnumVariant);
50 return (IEnumerator)EnumeratorToEnumVariantMarshaler.GetInstance(null).MarshalNativeToManaged(Marshal.GetIUnknownForObject(enumVariant));
53 public object GetUnderlyingObject()