Han Han - 1 month ago 22
C# Question

How to use a specific interface on a C# object in IronPython

I have a C# class that implements 2 IEnumerable interfaces. How can I access either interface from IronPython?

My class:

public class MyClass : IEnumerable<TypeA>, IEnumerable<TypeB>
{
IEnumerator<TypeA> IEnumerable<TypeA>.GetEnumerator()
{
return _lstTypeA.GetEnumerator();
}

IEnumerator<TypeB> IEnumerable<TypeB>.GetEnumerator()
{
return _lstTypeB.GetEnumerator();
}
}


I tried the following in Python, but although it runs without errors it does not return any elements from the IEnumerable interface:

x = MyClass()
xA = clr.Convert(x, IEnumerable[TypeA])
for y in xA: print y

Answer

As explained here you have to call methods and properties like using reflection (that is actually what it does under the hood).

In your case you should do:

x = MyClass()
enumerator = IEnumerable[TypeA].GetEnumerator(x)

then you can loop over enumerator:

for y in enumerator:
   print y