如何在IronPython中的C#对象上使用特定接口 [英] How to use a specific interface on a C# object in IronPython

查看:47
本文介绍了如何在IronPython中的C#对象上使用特定接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实现2个IEnumerable接口的C#类.如何从IronPython访问任一接口?

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

我的课:

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

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

我在Python中尝试了以下操作,但是尽管它运行时没有错误,但它不会从IEnumerable接口返回任何元素:

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

推荐答案

您需要在使用反射时调用方法和属性(实际上是在幕后发生的事情).

You need to call methods and properties as you were using reflection (that is actually what it happens under the hood).

在您的情况下,您应该这样做:

In your case you should do:

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

然后您可以遍历enumerator:

for y in enumerator:
   print y

这篇关于如何在IronPython中的C#对象上使用特定接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆