65435

FileStream is not marked as serializable, but the whole class is?

I have a class definition in separate assembly. The class is marked as serializable:

namespace example { [Serializable] public class my_class { public List<string> text; public FileStream audio; public Image img; public string nickname; } }

I can load this assembly and create an instance of this class with no problem. But when i try to cast to byte[] using code bellow

private byte[] ToByteArray() { if (send == null) // 'send' is a my_class instance; return null; BinaryFormatter bf = new BinaryFormatter(); bf.Binder = new Binder(); bf.AssemblyFormat = System.Runtime.Serialization.Formatters.FormatterAssemblyStyle.Full; bf.Binder.BindToType(example_assembly.FullName, "my_class"); MemoryStream ms = new MemoryStream(); bf.Serialize(ms, send); return ms.ToArray(); }

i get:

System.Runtime.Serialization.SerializationException -> Type System.IO.FileStream in Assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' is not marked as serializable.

I don't understand this because whole class is marked as serializable. Any sugestions ??

Answer1:

The Serializable attribute simply indicates a class can be serialized. It does not change the underlying functionality of a class. You can mark non-serializable classes and members as serializable.

A FileStream is not serializable. Marking it as such will not change that.

http://msdn.microsoft.com/en-us/library/system.serializableattribute(v=vs.110).aspx

Answer2:

Members are serializable, but their types should also be serializable. FileStream is not. You can implement ISerializable interface in your class and manully serialize audio/img fields.

Answer3:

The problem is that my_class is decorated with the [SerializableAttribute], but the class FileStream is not. =/

You can skip the property serialization by decorating the FileStream property with a [NonSerializedAttribute], but you can't serialize a file Stream.

This is not very beautiful, but this guy here and here converted the stream to a string. You could control the serialization of your class by implementing the ISerializale interface and parse the FileStream to something that is Serializable.

Recommend

  • BinaryFormatter ignore assembly version
  • Building Blender for breakpoints/Debug in Xcode
  • Google App Engine connection request timeout error
  • Generic Spray-Client
  • How can I make a XStreamMarshaller skip unknown binding?
  • How to format a list of dates using JsonConverter attribute [duplicate]
  • nokia Imaging SDK customize BlendFilter
  • Reading old clipboard data in new program version in C#
  • android bind to AccessibilityService
  • Spring 3 Collection Binding
  • guice assisted inject + multibinding + generics
  • SerialForms.pas(17): W1010 Method 'Create' hides virtual method of base type 'TCompon
  • How to make a post call to a Web Api Action?
  • Complex Object won't return when using 'include' syntax in WCF with Entity Version 6
  • What's the alternative to IValueFormatter in AutoMapper?
  • Jackson Java 8 DateTime serialisation
  • Powershell Hash Table to HTML
  • runtime error when linking ffmpeg libraries in qt creator
  • Can you pass an array from javascript to asp.net mvc controller action without using a form?
  • Compress a file with GZipStream while maintaining its meta-data
  • Ruby on Rails App deployed to heroku showing “We're sorry, but something went wrong”
  • Cannot get text from text area
  • Detecting null parameter in preprocessor macro
  • Sending keystrokes/mouse clicks to a Java program with Autohotkey
  • Why cepheus don't send int without quotes to orion?
  • C# program and C++ DLL compiled for 32-bit system crash on 64-bit system
  • iOS: Detect app start via notification press
  • Email verification using google app script and google forms
  • Get data from AJAX - How to
  • Using variable in a value field in jMeter
  • C# - Is there a limit to the size of an httpWebRequest stream?
  • Updating server-side rendering client-side
  • Validaiting emails with Net.Mail MailAddress
  • Timeout for blocking function call, i.e., how to stop waiting for user input after X seconds?
  • How to pass list parameters for each object using Spring MVC?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Proper folder structure for lots of source files
  • trying to dynamically update Highchart column chart but series undefined
  • How does Linux kernel interrupt the application?
  • sending mail using smtp is too slow