开发者

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

开发者 https://www.devze.com 2022-12-29 10:11 出处:网络
I want to parse a text message and store in into an object of type Sys开发者_JAVA百科tem.Net.Mime.Attachment. The problem comes when I want to serialize this object.

I want to parse a text message and store in into an object of type Sys开发者_JAVA百科tem.Net.Mime.Attachment. The problem comes when I want to serialize this object.

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

How can I avoid this?

Thanks.


You won't be able to do a simple serialization here because the class itself is not marked with the [Serializable] attribute.

However, after looking at the docs, it looks like the class is really just a helper for constructing and manipulating strings like "text/javascript". And based on the documentation of the ToString method, you can round trip a ContentType object merely using the ToString method and the constructor.

For 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));

you can do whatever you want with your string above (write it to disk...whatever).

0

精彩评论

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