.NET - 为什么只配置在单元测试中允许的标准输出?单元、标准、测试中、NET

由网友(含笑记下)分享简介:首先,我想指出的是,我真的不希望配置标准输出的......我只是想知道我为什么看到所描述的行为。我没有写坏code描述。First of all, I want to point out that I do not really want to dispose of the standard output...I j...

首先,我想指出的是,我真的不希望配置标准输出的......我只是想知道我为什么看到所描述的行为。我没有写坏code描述。

First of all, I want to point out that I do not really want to dispose of the standard output...I just want to know why I'm seeing the described behavior. I didn't write the bad code described.

我使用.NET 4的单元测试,并为code正在测试.NET 3.5。我使用MSTest的用于.NET 4为我的测试框架。

I'm using .NET 4 for the unit tests and .NET 3.5 for the code being tested. I'm using MSTest for .NET 4 as my testing framework.

最近我一直在努力与被扔,由于处置标准错误输出的错误错误的库。 (请参阅LibTiff.NET ReadDirectory是给System.ObjectDisposedException只在单元测试)。

Recently I have been working with a library that was throwing errors due to the blunder of disposing of the standard error output. (See LibTiff.NET ReadDirectory is giving System.ObjectDisposedException Only During Unit Tests).

这是相对他们的code看起来像:

This is relatively what their code looked like:

using (TextWriter stderr = Console.Error)
{
    ...
}

基本上,当不运行单元测试,标准的输出不设置,即使一个具体处置,但运行单元测试时,它是允许的。

Basically, when not running unit tests, the standard output is not disposed even if one specifically disposes of it, but when running unit tests, it is allowed.

任何人都可以解释为什么在单元测试的情况下?

推荐答案

这是一个释放的对象调用一个方法会抛出的ObjectDisposedException 。例如:

Calling a method on a disposed object will throw ObjectDisposedException. E.g.:

var textWriter = Console.Error;
textWriter.Dispose();
textWriter.WriteLine("Test");

最后一行应抛出异常。除了它不总是到

The last line should throw an exception. Except it doesn't always to that.

如果你仔细阅读BCL源$ C ​​$ C,你可以看到控制台或者使用的StreamWriter (真同步流作家)被挂接到一个真正的流(如控制台错误流),或者说是不提供给 StreamWriter.Null

If you peruse the BCL source code you can see that Console either uses a StreamWriter (really a synchronized stream writer) that is hooked up to either a "real" stream (e.g. the console error stream) or if that is not available to StreamWriter.Null.

真正的的StreamWriter 的结构是一种特殊的方式,以便它不是可关闭。这意味着,即使你关闭(或处置它),它只是继续正常工作。

The "real" StreamWriter is constructed in a special way so that it is not closeable. This means that even if you close it (or dispose it) it just continues to function as expected.

所以,如果你有一个真实的控制台流,你可以关闭 Console.Error 多次,只要你想,无需关闭底层流。你也不会得到任何的ObjectDisposedException

So if you have a "real" console stream you can close Console.Error as many times as you want without closing the underlying stream. Also you wont get any ObjectDisposedException.

如果有连接到 Console.Error 结束没有真正的流的StreamWriter 将关闭底层流(在这种情况下, Stream.Null )不表现出特殊的非可关闭的行为,如果你尝试使用的StreamWriter 以后你会得到一个的ObjectDisposedException

If there is no "real" stream attached to Console.Error closing the StreamWriter will close the underlying stream (in this case Stream.Null) which doesn't exhibit the special non-closeable behavior, and if you try to use the StreamWriter later you will get an ObjectDisposedException.

的底线是,你可以得到一个的ObjectDisposedException 如果您在应用程序中pmaturely关闭控制台流作家$ P $不具有真正的控制台流。

The bottom line is that you can get a ObjectDisposedException if you close a console stream writer prematurely in an application that doesn't have a real console stream.

上面还的信息适用于 Console.Out

阅读全文

相关推荐

最新文章