本文介绍了在Matlab中迭代C#迭代器(IEnumerable)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回大量对象的C#方法.这将在Matlab中使用.

I have a C# method that returns very large number of objects. This is to be consumed in Matlab.

namespace MyNameSpace{
    public static class MyClass{
        public static IEnumerable<MyDataObject> GetVeryLargeResponse(){
            while(CheckForSomeFunkyConditionThatsRarelyTrue()){
               yield return GetMyNextDataObject();
            }
            yield break;
        }
    }
}

在Matlab中打电话时

In Matlab when I make a call

result = MyClass.GetVeryLargeResponse();

我希望结果为IEnumerable<MyDataObject>类型,以便能够通过调用result.GetEnumerator()获得Enumerator<MyDataObject>.

I would expect result to be of type IEnumerable<MyDataObject>, so as to be able to get the Enumerator<MyDataObject> by calling result.GetEnumerator().

当我得到result时,它是MyNameSpace.<GetVeryLargeResponse>d_3类型,没有可用的GetEnumerator()方法.我确实看到result的Super类之一是System.Collections.Generic.IEnumerable<MyClass>.

Where as I'm getting result which is of type MyNameSpace.<GetVeryLargeResponse>d_3 with no GetEnumerator() method available. I do see one of result's Super class being System.Collections.Generic.IEnumerable<MyClass>.

有没有一种方法可以在Matlab中进行迭代,甚至可以将result投射"到Matlab中的IEnumerable<MyClass>.

Is there a way I can iterate over this in Matlab or even to 'cast' result to IEnumerable<MyClass> in Matlab.

p.s.

  1. 由于数据量大,转换为Array/IList等是不可行的
  2. 这不是在Matlab中如何遍历C#IEnumerable的重复项?,因为这正在处理IQueryable专门.
  3. 我正在使用Matlab 2010b
  1. Converting to Array / IList etc is not feasible due to data volume
  2. This is not duplicate of How can I iterate over a C# IEnumerable in Matlab?, as that is dealing with IQueryable specifically.
  3. I'm using Matlab 2010b

推荐答案

结果确实具有GetEnumerator()方法-可以通过显式接口实现来实现.

The result does have a GetEnumerator() method - it's just may be implemented with explicit interface implementation.

如果Matlab不愿意处理这个问题,您可以随时编写自己的映射类型和扩展方法来简化操作:

If Matlab isn't willing to handle that, you could always write your own mapping type and an extension method to make things simpler:

public static class Extensions
{
    public static EnumerableWrapper<T> Wrap<T>(this IEnumerable<T> source)
    {
        return new EnumerableWrapper<T>(source);
    }
}

public class EnumerableWrapper<T> : IEnumerable<T>
{
    private readonly IEnumerable<T> source;

    public EnumerableWrapper(IEnumerable<T> source)
    {
        this.source = source;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return new EnumeratorWrapper<T>(source.GetEnumerator());
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class EnumeratorWrapper<T> : IEnumerator<T>
{
    private readonly IEnumerator<T> source;

    public EnumeratorWrapper(IEnumerator<T> source)
    {
        this.source = source;
    }

    public T Current { get { return source.Current; } }

    object IEnumerator.Current { get { return Current; } }

    public bool MoveNext()
    {
        return source.MoveNext();
    }

    public void Reset()
    {
        source.Reset();
    }

    public void Dispose()
    {
        source.Dispose();
    }

}

然后尝试:

result = MyClass.GetVeryLargeResponse().Wrap();

对于Matlab来说,虽然不如此开箱即用,这似乎很奇怪...

It seems very odd for Matlab not so support this out of the box though...

这篇关于在Matlab中迭代C#迭代器(IEnumerable)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-23 03:24