是否有一种方法可以在运行时使对象可序列化?我应该使用反射吗?
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.
更多推荐
使对象在运行时可序列化
发布评论