I’m currently trying to make a C# API visible in COM and I’m currently stuck on making the non generic GetEnumerator method visible in COM for classes that also inherit IEnumerable<T>. If I implement another method on the ITestIEnumerable, it is visible. I’ve also found that non generic GetEnumerator methods are visible on classes that derive from Lists.
I don’t really understand why this isn’t working, it seems to be a special case for classes that inherit IEnumerable<T>. Is there anyway to expose this to COM so I can use the For Each construct in VB6?
For example
[ClassInterface(ClassInterfaceType.AutoDual)]
[ProgId("Test")]
[Guid("1aa2bdec-0530-44e8-b62d-b75914a6de22")]
public class TestIEnumerable : ITestIEnumerable
{
#region IEnumerable<Item> Members
[ComVisible(false)]
public IEnumerator<Item> GetEnumerator()
{
return _collection.GetEnumerator();
}
#endregion
#region ITestIEnumerable Members
[DispId(-4)]
[ComVisible(true)]
IEnumerator ITestIEnumerable.GetEnumerator()
{
return _collection.GetEnumerator();
}
#endregion
#region IEnumerable Members
[DispId(-4)]
[ComVisible(true)]
IEnumerator IEnumerable.GetEnumerator()
{
return _collection.GetEnumerator();
}
#endregion
}
[InterfaceType(ComInterfaceType.InterfaceIsDual)]
[Guid("187f3fed-9d4a-4a52-9a7c-a2fdeb56b9fa")]
[ComImport]
public interface ITestIEnumerable : IEnumerable<Item>
{
[DispId(-4)]
[ComVisible(true)]
new IEnumerator GetEnumerator();
}
You can’t have more than one public method of the same name with the same signature. As such, two of your
GetEnumeratormethods are being implemented using explicit interface implementation:As such, they’re treated as private members – it’s not even visible to other .NET code unless that code casts your
TestIEnumerableobject toITestIEnumerable.So, decide which one of your
GetEnumerator()methods you wish to expose, and make it public:And switch the generic one to an explicit implementation: