开发者

.NET iterator to wrap throwing API

I have a class with an API that allows me to ask for objects until it throws an In开发者_如何转开发dexOutOfBoundsException.

I want to wrap it into an iterator, to be able to write cleaner code. However, I need to catch the exception to stop iterating:

static IEnumerable<object> Iterator( ExAPI api ) {
    try {
       for( int i = 0; true; ++i ) {
          yield return api[i]; // will throw eventually
       }
    } 
    catch( IndexOutOfBoundsException ) {
       // expected: end of iteration.
    }
}

But...

When used with expression, a yield return statement cannot appear in a catch block or in a try block that has one or more catch clauses. For more information, see Exception Handling Statements (C# Reference).Statements (C# Reference). (from the msdn)

How can I still wrap this api?


You simply need to move the yield return statement outside of the try block, like this:

static IEnumerable<object> Iterator( ExAPI api ) {
   for( int i = 0; true; ++i ) {
        object current;
        try {
            current = api[i];
        } catch(IndexOutOfBoundsException) { yield break; }

        yield return current;
    } 
}


You can wrap the simple operation of getting the object into a separate function. You can catch the exception in there:

bool TryGetObject( ExAPI api, int idx, out object obj )
{
    try
    {
        obj = api[idx];
        return true;
    }
    catch( IndexOutOfBoundsException )
    {
        return false;
    }        
}

Then, call that function and terminate if necessary:

static IEnumerable<object> Iterator( ExAPI api )
{
   bool abort = false;

    for( int i = 0; !abort; ++i )
    {
        object obj;
        if( TryGetObject( api, i, out obj ) )
        {
            yield return obj;
        }
        else
        {
            abort = true;
        }
    }
}


Just reorder the code:

static IEnumerable<object> Iterator( ExAPI api ) {
    bool exceptionThrown = false;
    object obj = null;
    for( int i = 0; true; ++i ) {
        try {
            obj = api[i];
        }
        catch( IndexOutOfBoundsException ) {
            exceptionThrown = true;
            yield break;
        }

        if (!exceptionThrown) {
            yield return obj;
        }
    }
}


If you can't check the bounds of the object at all, you could do something like this

static IEnumerable<object> Iterator( ExAPI api )
{
 List<object> output = new List<object>();
    try
 {
  for( int i = 0; true; ++i )
   output.Add(api[i]);
    } 
    catch( IndexOutOfBoundsException )
 {
  // expected: end of iteration.
    }
 return output;
}

although now that im looking here, the answer above is better i believe. The one SLaks posted.


    static IEnumerable<object> Iterator(ExAPI api)
    {
        int i = 0;
        while (true)
        {
            Object a;
            try
            {
                a = api[i++];
            }
            catch (IndexOutOfBoundsException)
            {
                yield break;
            }
            yield return a;
        }
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜