开发者

How can I access an existing byte[] in C#?

I have a small issue accessing a byte[]:

I have a binary object (byte[] saved to mssql db) which I get from the db and I want to read it. Whenever I access it, for its length or for its Read() method, I get a Cannot access a closed Stream exception.

What's the best way to treat binaries if they have to be updated in the code and than saved again to the db?

Thanks.

Edit - code

In this application we convert a test object to a generic data object we've created to simplify, so this is the data object:

public class DataObject
{
    public Stream Content { get; set; }
    public Descriptor Descriptor { get; set; }
}

The descriptor contains metadata only (currently only name and description strings) and is not relevant, I think. The test is more complicated, I'll start by adding the mapping into data object. The serializer mentioned is NetDataContractSerializer.

public DataObject Map(Test test)
{
    using(var stream = new MemoryStream())
    {
        Serialize(test, stream);
        return new DataObject { Content = stream, Descriptor = test.Descriptor };
    }
}
 private void Serialize(Test test, MemoryStream stream)
 {
    serializer.WriteObject(stream, test);
    stream.Flush();
    stream.Position = 0;
 }

and vice versa:

public Test Build(DataObject data)
{
      using (var stream = data.Content)
      {
           var test = Deserialize(stream);
           test.Descriptor = data.Descriptor;
           return test ;
      }
}
private Test Deserialize(Stream stream)
{
    return serializer.ReadObject(stream) as IPythonTest;
}  

Edit II - trying to change the test's content:

This is my first attempt handling streams, I'm not sure I'm doing it right, so I'll explain first what I want to do: The information in data field should be saved into the test's data object.

private static void UpdateTestObject(DataObject data, Test test)
{
    var testData = new byte[data.Content.开发者_Go百科Length];
    data.Content.Read(testData, 0, (int) data.Content.Length);
    test.TestObject = testData;
}

The exception is thrown in UpdateTestObject when accessing data.Content. I get it after creating some test, mapping it, and trying to save it.


data.Content.Read(testData, 0, (int) data.Content.Length);

Here we go. The data object has a Content stream that it closed.

Result: Error.

Reasno? TOTALLY (!) unrelated to your question. Basically find out why / what is the problem there in your data handling.

Could be a design fubar in which the stream is not available after a certain point and youru sage of the object is past this point.


So the problem is caused by the Map() method - as far as I could understand, since it used:

using (var stream = new MemoryStream())
{ ... }

The stream was disposed of at the end of the block. Changing it to declaring a MemoryStream and then using it afterwards worked.
Thanks to everyone who gave it a thought (not mentioning reading all this code)! :)

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜