How to serialize an object of type System.Net.Mime.ContentType?

I want to parse a text message and save it into an object of type System.Net.Mime.Attachment. The problem occurs when I want to serialize this object.

Error: Type "System.Net.Mime.ContentType" is not marked as serializable.

How can i avoid this?

Thank.

+1
source share
1 answer

You cannot do simple serialization here because the class itself is not marked with the [Serializable] attribute.

However, looking at the docs , it seems that the class is indeed an assistant for building and managing strings of the "text / Javascript" type. And based on the documentation of the ToString method, you can bypass the ContentType object using the ToString method and constructor.

Example:

ContentType ctype = ....;//your content type object
String serialized_form = ctype.ToString();
//save the string to whatever medium you like
...
ContentType ctype2 = new ContentType(serialized_form);
Debug.Assert(ctype.Equals(ctype2));

, , ( ... ).

+1

All Articles