I have one debugger visualizer for seeing list of class object in the form of data table. But the limitation for the code is that the class should be serializable i.e. should be marked as [Serializable] and if the class is not marked Serializable then the debugger crashes. So, can anybody tell me how to make a class Serializable at run time if the class is not marked Serializable.
5 Answers
You cannot modify the metadata of an existing class at runtime.

- 1,023,142
- 271
- 3,287
- 2,928
-
Is there any alternative to that or there is no way to achieve this? – Ashwani K Mar 17 '10 at 10:38
-
1@Ashwani, you could create another class which encapsulates the real class and which is marked with `Serializable` and then work with this class. – Darin Dimitrov Mar 17 '10 at 10:40
-
You're not talking about the difference between debug mode and release mode are you? – Andrew Bienert Mar 17 '10 at 10:40
-
@Andrew Bienert: No, like we can the see the data of a data table in data set visualizer, I created my own visualizer for list of class object to show list data in data table. – Ashwani K Mar 17 '10 at 10:44
Any class with public get/set properties is XmlSerializable. Can you use XML serializers instead ?

- 2,637
- 1
- 17
- 15
The fact that a class is missing [Serializable] can be explained two ways. It might be an error of omission, the more common case. Or the class can simply not support serialization. Which is not unusual, classes often depend on state that cannot be faithfully reproduced at deserialization time because it depends on global program state. Any of the Windows Forms controls would be a good example, they can't be deserialized without having a native Windows window that is in the required state, a state that often requires other windows to be created as well (like the container window) and many messages.
Well, this isn't going to help you implement your visualizer. You can't reliably implement it with serialization. Using reflection however gives you access to the same property and field values. And reflection is always supported.

- 922,412
- 146
- 1,693
- 2,536
If a class is not marked [Serializable]
, you can try serialization using SerializationSurrogate

- 11,468
- 16
- 63
- 89
-
This still does not solve my problem, but it helped me in learning run time serialization. Thanks. – Ashwani K Apr 02 '10 at 07:06
-
1Broken link..... https://msdn.microsoft.com/en-us/library/system.runtime.serialization.surrogateselector(v=vs.110).aspx – Brent Nov 27 '15 at 20:23
You could look at your question differently, using [Serializable] allows you to use the dotnet libraries to serialize into json, xml etc. You can still serialize by writing your own methods to do so as fundamentally just about any data structure can be represented in xml or json format.
Adding [Serializable] to classes is one of the best practice tips in Bill Wagner's brilliant book Effective C#: 50 specific ways to improve your C#.
You can serialize a class without it being [Serializable] as @Darin (+1) pointed out you can't retrospectively redecorate a class. If I were you I'd put in [Serializable] as working around this is not worth the effort.

- 8,919
- 4
- 38
- 59
-
Thanks. Just out of curiosity, does putting [Serializable] attribute for a class has any performance hit? I mean should we include serializable attribute for any class which we create? – Ashwani K Mar 17 '10 at 10:51
-
@Ashwani Bill Wagner recommends the practice in Effective C# - I don't have the book to hand, but when I read the section on why it should be the default the argument was compelling. This answer on SO confirms that there is no performance hit - http://stackoverflow.com/questions/1402903/does-adding-serializable-to-the-class-have-any-performance-implications – amelvin Mar 17 '10 at 12:18