Strongly typed collection for use in both COM and CLR

I am trying to implement a typed collection that is intended for use in .NET and COM (Windows Script Host).

MSDN states that it is necessary to implement IEnumerable to make the collection available to VB / VBS For Each:

[ComVisible] [Guid] interface IMyCollection : IEnumerable { new IEnumerator GetEnumerator(); // required because of well-known COM interop inheritance issue } 

This works fine in VBScript / JScript, but for the CLR / C # foreach statement this collection seems untyped.

I tried to make the collection interface strongly typed as follows. Now the collection works fine in C #, but causes an error in VB, since the _NewEnum method (with id (-4)) is missing in the TLB

 [ComVisible] [Guid] interface IMyCollection : IEnumerable<IMyItem> { } 

The following code works fine in VBScript, but again makes the build untyped for .NET clients:

 [ComVisible] [Guid] interface IMyCollection : IEnumerable<IMyItem> { new IEnumerator GetEnumerator(); } 

Also tried to implement _NewEnum manually, returning an object that implements System.Runtime.InteropServices.ComTypes.IEnumVARIANT , but without success. The method was exported to TLB using the tlbexp utility, but without the MIDL id attribute.

How can i solve this?

+4
source share
1 answer

Have you tried making IMyItem "ComVisible"? If it is not ComVisible, I think IMyItem will be recognized as VARIANT with untyped.

0
source

Source: https://habr.com/ru/post/1487622/


All Articles