如何解决如何从IronRuby调用C#IEnumerable
| 我有一个C#方法: public static IEnumerator getPixels(Picture picture) {
for (int x=0; x < picture.width; x++) {
for (int y=0; y < picture.height; y++) {
yield return picture.getPixel(x,y);
}
}
}
我可以在IronPython中称呼它为:
for pixel in getPixels(pic):
r,g,b = getRGB(pixel)
gray = (r + g + b)/3
setRGB(pixel,gray,gray)
但是我看不到如何从IronRuby调用它:
Myro::getPixels(pic) do |pixel|
r,b = Myro::getRGB pixel
gray = (r + g + b)/3
Myro::setRGB(pixel,gray)
end
我回来的只有Graphics+<getPixels>c__Iterator0.
我需要做些什么才能真正获得IronRuby中的每个像素并进行处理?
解决方法
来自Jimmy Schementi:
http://rubyforge.org/pipermail/ironruby-core/2011-May/007982.html
如果将getPixels的返回类型更改为IEnumerable,则可以这样做:
Myro::getPixels(pic).each do |pixel|
...
end
并且可以说它应该是IEnumerable而不是IEnumerator,因为
IEnumerable.GetEnumerator()给您一个IEnumerator。
您的代码示例将闭包传递给getPixels方法,该方法只是
被忽略(所有Ruby方法在语法上都接受阻止/关闭,
并且他们可以选择使用它),并返回IEnumerator。
如今,IronRuby不支持将Ruby \的Enumerable模块映射到
IEnumerator对象,因为返回IEnumerator有点尴尬
而不是来自公共API的IEnumerable,但是由于IronPython
设置支持它的优先级,我们应该研究它。开了
http://ironruby.codeplex.com/workitem/6154。
〜吉米
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。