使对象在运行时可序列化 [英] Make an object serializable at runtime

查看:45
本文介绍了使对象在运行时可序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有一种方法可以在运行时使对象可序列化?我应该使用反射吗?

Is there a way to make serializable an object at runtime? Should I use Reflection?

注意:该对象是外部库的一部分,所以我没有它的源代码.

Note: the object is part of an external library, so I do not have the source code for it.

推荐答案

tl; dr-否

虽然可以在运行时使用 TypeDescriptor 向任何内容添加属性,但是在 Serializable 属性的上下文中,由于 BinaryFormatter 使用经典反射,而不使用 TypeDesciptor 反射.

While you can use TypeDescriptor to add attributes to anything at runtime, in the context of the Serializable attribute it won't help since BinaryFormatter uses classic reflection and not TypeDesciptor reflection.

该属性背后的想法是让可序列化的类选择接受可序列化.有关详细信息,请此处.

The idea behind that attribute is for the serializable class to opt-in to being serializable. More details about this here.

这篇关于使对象在运行时可序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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