Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to exclude specific types from serialization?

I run sgen against my assembly with a metric ton of types. I want to exclude 2 types from serialization. I don't seem to be able to find a way to do it.

I see that sgen has a /type switch to specify a specific type, but nothing to exclude a specific type.

Is there a way to exclude specific types from serialization?

like image 987
AngryHacker Avatar asked Jul 19 '12 21:07

AngryHacker


2 Answers

You can try changing the access of the classes you want to exclude from Xml Serialization by marking the class as internal, then sgen.exe should skip that class.

internal class NotToBeSerialized
{
    ...
}
like image 118
Mikael Engver Avatar answered Oct 05 '22 15:10

Mikael Engver


To prevent the class being included in sgen processing, ensure it doesn't have a parameterless constructor.

As explained by the answer to this question Why XML-Serializable class need a parameterless constructor, serialization requires a parameter less constructor, of any permission level, to work. Making the paramaterless constructor private isn't sufficient, to exclude if from sgen processing.

like image 33
Tom Avatar answered Oct 05 '22 15:10

Tom