在返回IEnumerable <t> </t>的方法中使用IDisposable对象

时间:2012-06-01 11:45:39

标签: c# linq resources

想象一下,你有一个内部使用IDisposable对象的方法(例如streamreader),并且yield会在从文件中读取时返回项目。像这样:

public IEnumerable<YourObject> Read(string filename)
{
    using(var filestream = new FileStream(filename, FileMode.Open))
    {
        using(var reader = new StreamReader(filestream))
        {
            string line;

            while((line = reader.ReadLine()) != null)
            {
                yield return new YourObject(line);
            }
        }
    }
}

当我使用不迭代完整集合的LINQ方法时,readerfilestream会被处理吗?

YourOjbect firstLine = Read("myfile.txt").First();

3 个答案:

答案 0 :(得分:27)

当您使用yield关键字编译器生成嵌套类时,该类实现IEnumerableIEnumeratorIDisposable并存储所有上下文数据:

[CompilerGenerated]
private sealed class <Read>d__0 : IEnumerable<YourObject>, IEnumerable, IEnumerator<YourObject>, IEnumerator, IDisposable
{
    // Fields
    private int <>1__state;
    private YourObject <>2__current;
    public string <>3__filename;
    public Foo <>4__this;
    private int <>l__initialThreadId;
    public FileStream <filestream>5__1;
    public string <line>5__3;
    public StreamReader <reader>5__2;
    public string filename;

    // Methods
    [DebuggerHidden]
    public <Read>d__0(int <>1__state);
    private void <>m__Finally4();
    private void <>m__Finally5();
    private bool MoveNext();
    [DebuggerHidden]
    IEnumerator<YourObject> IEnumerable<YourObject>.GetEnumerator();
    [DebuggerHidden]
    IEnumerator IEnumerable.GetEnumerator();
    [DebuggerHidden]
    void IEnumerator.Reset();
    void IDisposable.Dispose();

    // Properties
    YourObject IEnumerator<YourObject>.Current { [DebuggerHidden] get; }
    object IEnumerator.Current { [DebuggerHidden] get; }
}

如您所见,yielding方法的上下文中的所有局部变量都被移动到此生成的类的字段中。有趣的方法是那些名字中有m_Finally的方法:

private void <>m__Finally4()
{
    this.<>1__state = -1;
    if (this.<filestream>5__1 != null)
    {
        this.<filestream>5__1.Dispose();
    }
}

如您所见,这些方法处理您的一次性对象(FileStreamStreamReader)。什么时候叫?在枚举结束时,或者在调用Dispose时:

private bool MoveNext()
{
    bool CS$1$0000;
    try
    {
        int CS$4$0001 = this.<>1__state;
        if (CS$4$0001 != 0)
        {
            if (CS$4$0001 != 3)
            {
                goto Label_00AB;
            }
            goto Label_0074;
        }
        this.<>1__state = -1;
        this.<filestream>5__1 = new FileStream(this.filename, FileMode.Open);
        this.<>1__state = 1;
        this.<reader>5__2 = new StreamReader(this.<filestream>5__1);
        this.<>1__state = 2;
        while ((this.<line>5__3 = this.<reader>5__2.ReadLine()) != null)
        {
            this.<>2__current = new YourObject(this.<line>5__3);
            this.<>1__state = 3;
            return true;
        Label_0074:
            this.<>1__state = 2;
        }
        this.<>m__Finally5();
        this.<>m__Finally4();
    Label_00AB:
        CS$1$0000 = false;
    }
    fault
    {
        this.System.IDisposable.Dispose();
    }
    return CS$1$0000;
}

void IDisposable.Dispose()
{
    switch (this.<>1__state)
    {
        case 1:
        case 2:
        case 3:
            try
            {
                switch (this.<>1__state)
                {
                    case 2:
                    case 3:
                        break;

                    default:
                        break;
                }
                try
                {
                }
                finally
                {
                    this.<>m__Finally5();
                }
            }
            finally
            {
                this.<>m__Finally4();
            }
            break;
    }
}

如果您期待First() Enumerable的实施,那么您会看到 - 它在返回第一项后调用Dispose

using (IEnumerator<TSource> enumerator = source.GetEnumerator())
{
   if (enumerator.MoveNext())
   {
       return enumerator.Current;
   }
}

因此将调用Dispose自动生成的类,并且所有需要处置的局部变量将通过调用m_Finally方法来处理。

BTW(不是关于使用LINQ)如果查看foreach statement implementation,您会看到枚举后枚举器被释放。因此,即使在Dispose或异常的情况下,也会调用生成的类break

答案 1 :(得分:17)

是的,他们被处置了。

[编辑] 只要您使用LINQ方法或foreach循环,就会自动处理。但是,如果您决定在方法上手动调用.Enumerator().MoveNext(),则需要自行处理。 [/编辑]

此代码:

class something : IDisposable
{
    public void Dispose()
    {
        Console.WriteLine("Disposing");
        Console.WriteLine(Environment.StackTrace);
    }
}
static IEnumerable<string> ie()
{
    using (new something())
    {
        Console.WriteLine("first");
        yield return "first";
        Console.WriteLine("second");
        yield return "second";
    }
}
static void Main(string[] args)
{
    Console.WriteLine("before");
    ie().First();
    Console.WriteLine("after");
}

打印:

before
first
Disposing
   at System.Environment.GetStackTrace(Exception e, Boolean needFileInfo)
   at System.Environment.get_StackTrace()
   at TestApp.Program.something.Dispose() in C:\Users\Tim\Documents\Visual Studi
o 2010\Projects\TestApp\TestApp\Program.cs:line 198
   at TestApp.Program.<ie>d__0.<>m__Finally2() in C:\Users\Tim\Documents\Visual
Studio 2010\Projects\TestApp\TestApp\Program.cs:line 0
   at TestApp.Program.<ie>d__0.System.IDisposable.Dispose() in C:\Users\Tim\Docu
ments\Visual Studio 2010\Projects\TestApp\TestApp\Program.cs:line 0
   at System.Linq.Enumerable.First[TSource](IEnumerable`1 source)
   at TestApp.Program.Main(String[] args) in C:\Users\Tim\Documents\Visual Studi
o 2010\Projects\TestApp\TestApp\Program.cs:line 214
   at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args
)
   at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySec
urity, String[] args)
   at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
   at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state, Boolean ignoreSyncCtx)
   at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, C
ontextCallback callback, Object state)
   at System.Threading.ThreadHelper.ThreadStart()
after

答案 2 :(得分:1)

这是一个普通的LINQ问题/问题,是的 - LINQ将在执行时处理它所获得的所有一次性元素。