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.
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 - No.
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.
Depending on the structure you could possibly use a serializer which is not opt-in i.e. it does not require any attribute on the target class, for example the XmlSerializer or Json.NET.