Serialization exceptions occurring in obfuscated assemblies
Published 02 January 2013
After using SmartAssembly on an assembly that serializes objects, XML or Serialization exceptions may occur.
Obfuscating serializable code with SmartAssembly, for the most part, will not break serialization of classes. There are some special situations to be aware of, however.
When obfuscating classes and methods in a DLL, SmartAssembly should not cause a problem.
Classes and properties with the public access modifier are not renamed. In order to be serialized, these classes are marked as public, so by default there should be no issues.
Classes and properties in an executable assembly, however, will be made private and renamed, breaking serialization if the class is defined in the executable. SmartAssembly attempts to work around this by automatically excluding from obfuscation all classes with the Serializable attribute set.
For example, this class can be serialized after being obfuscated by SA:
[Serializable] public class MyClass { }
Alternatively, you can exclude the class from serialization from within the SA project if you do not wish to modify your own code.
The reason for excluding classes and properties from obfuscation is that .NET Reflection technology is used to find the class and property names in order to create XML elements with the proper name. Therefore, if it's absolutely necessary to obfuscate classes that you want to serialize, you can work around this by implementing ISerializable on the class. Using this strategy, you must ensure all dependent classes also implement ISerializable on their properties or are excluded from obfuscation.
[Serializable] public class MyClass : ISerializable { public List ListOfThings; #region ISerializable Members public void GetObjectData(SerializationInfo info, StreamingContext context) { info.AddValue("ListOfThings", ListOfThings); } #endregion
The Pruning function of SmartAssembly will destroy metadata necessary for both serialization and deserialization. For instance, any properties of your serializable class will cease to exist, as far as serialization is concerned. All classes referenced by properties of the serialized class, and their children, must be excluded from pruning either in the SA project, or using the DoNotPrune or DoNotPruneType attribute.
[Serializable, SmartAssembly.Attributes.DoNotPruneType] public class MyClass