tomblog - Il blog di Tommaso Caldarola

http://entombloged.blogspot.com
posts - 37, comments - 59, trackbacks - 1

IEnumerator.Reset può lanciare NotSupportedException

Come descritto su MSDN il metodo reset è fornito solo per interoperabilità con il mondo COM; testualmente

The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException.

Come esempio provate ad eseguire questo semplice test:

class test
{
    public void start()
    {
        this.Reset(this.GetItems());
        this.Reset(this.GetYieldItems());
    }

    private void Reset<T>(IEnumerable<T> items)
    {
        items.GetEnumerator().Reset();
    }

    private IEnumerable<string> GetItems()
    {
        return new List<string>(new string[] { "0", "1", "2" });
    }

    private IEnumerable<string> GetYieldItems()
    {
        for (int i = 0; i < 3; i++)
        {
            yield return i.ToString();
        }
    }
}

Lanciare il test con

new test().start()

Il metodo GetYieldItems lancia l'eccezione su citata proprio perché l'implementazione fornita dal framework dello yield, generata a compile time, non prevede il metodo Reset (Reflector conferma).

Print | posted on lunedì 25 agosto 2008 14.11 | Filed Under [ .net ]

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 8 and 2 and type the answer here:

Powered by: