How to exclude certain types from serialization?

I run sgen against my build with metric tons of types. I want to exclude 2 types from serialization. I can't seem to find a way to do this.

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

Is there a way to exclude certain types from serialization?

+9
source share
4 answers

Besides putting the types you want to exclude into another assembly, you cannot exclude types from the serializer generation.

Refresh

Other posters offer additional options for excluding certain types with different applicability depending on your use case.

+3
source

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

 internal class NotToBeSerialized { ... } 
0
source

To prevent the class from being included in sgen processing, make sure that it does not have a constructor without parameters.

As explained in the answer to this question, why does the XML-Serializable class need a constructor without parameters , for serialization, a constructor without parameters of any permission level is required. Creating a private constructor without parameters is not enough to exclude it from sgen processing.

0
source

Not sure if you are looking for this, but you can exclude your classes from serialization by specifying [NonSerialized] before the class definition. Therefore, if you want to exclude a specific type, you will have to inherit it and create your own class

 [NonSerialized] public class Point { public int x, y; } 
-6
source

Source: https://habr.com/ru/post/920920/


All Articles