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

依然范特西╮ 提交于 2019-12-11 11:33:16

问题


I want to parse a text message and store in into an object of type System.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.


回答1:


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).



来源:https://stackoverflow.com/questions/2795588/how-to-serialize-an-object-of-type-system-net-mime-contenttype

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!