How to exclude specific types from serialization?

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.


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
{
    ...
}

Apart from putting the types you wish to exclude in a different assembly, you cannot exclude types from the serializer generation.

Update

Other posters have come up with additional possibilities to exclude specific types, with varying applicability based on your use case.