如何序列化System.Net.Mime.ContentType类型的Object? [英] How to serialize an Object of type System.Net.Mime.ContentType?
问题描述
我想解析一条文本消息并将其存储到System.Net.Mime.Attachment类型的对象中.当我要序列化此对象时,问题就来了.
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.
如何避免这种情况?
谢谢.
推荐答案
您将无法在此处进行简单的序列化,因为该类本身未标记有[Serializable]属性.
You won't be able to do a simple serialization here because the class itself is not marked with the [Serializable] attribute.
但是,在查看文档之后,看起来该类实际上只是构造和处理"text/javascript"之类的字符串的助手.并且基于ToString方法的文档,您可以仅使用ToString方法和构造函数来往返ContentType对象.
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.
例如:
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).
这篇关于如何序列化System.Net.Mime.ContentType类型的Object?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!