如何序列化System.Net.Mime.ContentType类型的Object? [英] How to serialize an Object of type System.Net.Mime.ContentType?

查看:82
本文介绍了如何序列化System.Net.Mime.ContentType类型的Object?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想解析一条文本消息并将其存储到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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆