2013-04-24 61 views
3

我有一个非封闭的流类,封装在带有二进制阅读器的使用块中,但由于某些原因块结束时,我的非封闭流仍然关闭。为什么我的非关闭流仍然关闭?

流被定义为:

internal class NonClosingStream : Stream, IDisposable 
{ 
    private Stream baseStream; 

    public NonClosingStream(Stream baseStream) 
    { 
     this.baseStream = baseStream; 
    } 

    public override bool CanRead{ get { return baseStream.CanRead; } } 
    public override bool CanSeek{ get { return baseStream.CanSeek; } } 
    public override bool CanWrite { get { return baseStream.CanWrite; } } 

    public override void Flush() 
    { 
     baseStream.Flush(); 
    } 

    public override long Length { get { return baseStream.Length; } } 

    public override long Position 
    { 
     get { return baseStream.Position; } 
     set { baseStream.Position = value; } 
    } 

    public override int Read(byte[] buffer, int offset, int count) 
    { 
     return baseStream.Read(buffer, offset, count); 
    } 

    public override long Seek(long offset, SeekOrigin origin) 
    { 
     return baseStream.Seek(offset, origin); 
    } 

    public override void SetLength(long value) 
    { 
     baseStream.SetLength(value); 
    } 

    public override void Write(byte[] buffer, int offset, int count) 
    { 
     baseStream.Write(buffer, offset, count); 
    } 

    public override void Close() 
    { 
     // Disconnects from base stream, but does not close it 
     this.baseStream = null; 
    } 

    void IDisposable.Dispose() 
    { 
     // Disconnects from base stream, but does not close it 
     this.baseStream = null; 
    } 
} 

和读取的块看起来是这样的:

public T Deserialize<T>(Stream stream) 
{ 
    using (NonClosingStream nonClosingStream = new NonClosingStream(stream)) 
    using (BinaryReader reader = new BinaryReader(nonClosingStream, Encoding.ASCII, true)) 
    { 
     // Read the type name, then convert it to an actual type 
     String typeName = reader.ReadString(); 
     Type graphType = AvailableTypes.GetType(typeName); 

     // If a deserializer for this type already exists, use it. 
     if (deserializerFunctions.ContainsKey(graphType)) 
     { 
      return (T)deserializerFunctions[graphType](reader); 
     } 

     // Otherwise, create one and use it 
     T graph = (T)FormatterServices.GetUninitializedObject(graphType); 

     typeof(ServiceSerializer).GetMethod("DeserializeObject", 
       BindingFlags.NonPublic | BindingFlags.Static) 

      .MakeGenericMethod(graphType) 
      .Invoke(this, new Object[] { reader, graph }); 

     return graph; 
    } 
} 

我究竟做错了什么?

更新

所以我写了这个小老爹:

static void Main() 
{ 
    MemoryStream stream = new MemoryStream(); 
    using (NonClosingStream nonCloser = new NonClosingStream(stream)) 
    using (BinaryWriter writer = new BinaryWriter(nonCloser)) 
    using (BinaryReader reader= new BinaryReader(nonCloser)) 
    { 
     writer.Write("Lorem ipsum"); 

     stream.Seek(0, SeekOrigin.Begin); 
     String data = reader.ReadString(); 

     Console.WriteLine(data); 
    } 

    stream.Seek(0, SeekOrigin.Begin); 

    using (NonClosingStream nonCloser = new NonClosingStream(stream)) 
    using (BinaryWriter writer = new BinaryWriter(nonCloser)) 
    using (BinaryReader reader = new BinaryReader(nonCloser)) 
    { 
     writer.Write("Lorem ipsum"); 

     stream.Seek(0, SeekOrigin.Begin); 
     String data = reader.ReadString(); 

     Console.WriteLine(data); 
    } 

    Console.ReadLine(); 
} 

,它似乎做工精细,像它应该流保持打开状态。所以我想这个共识是对的。不知何故,我正在关闭其他地方的流。当我弄清楚我会发布结果。谢谢大家。

更新

Gaaaahhh,我想通了这个问题。因此,代码的工作方式是,在对对象进行序列化/反序列化时,它会从表达式树中构建一个定制的序列化程序,然后对其进行编译,以便将来的序列化更加流畅。这意味着我的代码充斥着这样的东西:

Action<BinaryReader, Object> assignmentAction = delegate(BinaryReader bReader, Object oGraph) 
{ 
    bReader.ReadByte();  // Read the next action 
    bReader.ReadString(); // Read the field name 
    bReader.ReadByte();  // Read the field type 

    // Call the assignment lambda 
    assignmentLambda(reader, deserializerFunctions[primitiveType], (T)oGraph); 
}; 

你有没有发现?没有?我也没有明显。让我们添加一些背景:

private static void DeserializeObject<T>(BinaryReader reader, T graph) 
{ 
    ... 

    Action<BinaryReader, Object> assignmentAction = delegate(BinaryReader bReader, Object oGraph) 
    { 
     bReader.ReadByte();  // Read the next action 
     bReader.ReadString(); // Read the field name 
     bReader.ReadByte();  // Read the field type 

     // Call the assignment lambda 
     assignmentLambda(reader, deserializerFunctions[primitiveType], (T)oGraph); 
    }; 

    ... 
} 

拉姆达从外部块关闭,而不是使用缓存的解串器运行时提供的bReader超过reader。因此,当反序列化器运行时,它使用的是已经废弃的二进制阅读器对象,而不是提供给它的新对象。我想这个问题不是我关闭了这个流,而是我使用了一个处理过的阅读器。至少这解释了为什么它会工作一次,然后第二次失败,因为第二次它依赖于缓存的解串器。哎呀!

谢谢大家。

+2

在这里看不到任何问题。你怎么知道它已关闭?你怎么知道它已经关闭*在这里*? – usr 2013-04-24 21:54:32

+0

因为如果我打开它们,它工作正常。 – sircodesalot 2013-04-24 21:56:12

+0

您如何知道基础流已关闭?之后你在做什么?读取序列化对象后,流中是否有更多数据? – antlersoft 2013-04-24 21:56:47

回答

0

它取决于您的NonClosingStream类包装的流是否在其他地方被引用。如果不是,那么基础流将没有引用,因此在某个时刻,其终结器将关闭流。

+0

如果没有进一步的引用,他不能*观察*正在关闭的流。 – usr 2013-04-24 21:58:04

+0

他可以,如果它是现在可访问的文件,因为不再有任何与它关联的进程。 – 2013-04-24 22:05:48

1

由于您的流不会创建内部流,因此最有可能的外部代码会关闭您的内部流。机会是你的代码的样子:

NonClosingStream nonClosing; 
using(var stream = new FileStream(...)) 
{ 
    nonClosing = new NonClosingStream(stream); 
    .... 
} 
// inner stream now closed and nonClosing will fail all operations. 
+0

我认为这是一个评论,因为它是高度推测。 – usr 2013-04-24 22:00:28

+0

这就是我期待的事情,但它怎么能为我关闭流? – sircodesalot 2013-04-24 22:00:36

+0

@usr - 很可能,不幸的是,代码在评论中是不可读的:( – 2013-04-24 22:01:06

1
void IDisposable.Dispose() 

你的类有的Dispose()方法。你明确实施的那个。以及您从Stream类继承的那个。问题是,BinaryStream不知道你的豆子。它只知道Stream实现的那个。此外,当您使用BinaryStream(Stream)构造函数时,BinaryStream对象将承担传递的Stream对象的所有权。这意味着它将在处理它自己时处置该流。也许你现在看到了这个问题,继承的Dispose()方法将被调用,而不是你的。它关闭了基础流。

这就是Stream实现Dispose模式的原因。您需要使其看起来像这样:

internal class NonClosingStream : Stream { 
    protected override Dispose(bool disposing) {} 
} 
+0

是的,我最初做到了,但行为没有任何区别。以某种方式使我的非关闭流(这没有意义),它不应该能够关闭'this.baseStream'。目前不在代码前面,但我明天再试一次。 – sircodesalot 2013-04-25 03:38:11