views:

278

answers:

5

Hello All:

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.

Thanks Ashwani

+1  A: 

You cannot modify the metadata of an existing class at runtime.

Darin Dimitrov
Is there any alternative to that or there is no way to achieve this?
Ashwani K
@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
You're not talking about the difference between debug mode and release mode are you?
Andrew Bienert
@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
A: 

Any class with public get/set properties is XmlSerializable. Can you use XML serializers instead ?

Seb
+1  A: 

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.

amelvin
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
@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
Thanks Amelvin.
Ashwani K
No problem at all.
amelvin
+1  A: 

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 simple not support serialization. Which is pretty common, 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.

Hans Passant
+1  A: 

If a class is not marked [Serializable], you can try serialization using SerializationSurrogate

ram
Thanks Ram. I will look into it
Ashwani K
This still does not solve my problem, but it helped me in learning run time serialization. Thanks.
Ashwani K