Linker vs Serialization

The linker does not always play well with serialization. By nature serializers are a bit too dynamic about what they do, making the linker’s job harder than usual. Each of them also has some specific requirements, e.g. for XML serialization:

  • a default (parameter-less) constructor must exists;
  • properties must have both a getter and a setter

Of course the linker can and will eliminate any .ctor, getter or setters if they are not directly used in your application. The static analysis does not cover how the serializers works – a bit of help is needed.

This is likely the major reason to avoid “Link all” when building MonoTouch applications. But sometimes it can also affect “Link only SDK assemblies”, when the required types are located inside the code that MonoTouch ships. E.g. some fixes were made inside the class libraries for Point[F], Size[F] and Rectangle[F] to allow them to work with XML serialization (i.e. ensuring the setters would not be linked away).

However the fixes are generally easy – you simply need to add some [Preserve] attributes to ensure the serialization contract is not broken by the linker. OTOH it works better when you have the source code and it’s not quite as fun without it.

The good news is that this should be even easier in the future, maybe even automatic for most cases. The linker used by MonoTouch 5.1 (next beta) now understands the basic requirements for the XML and data contract serializers. So code decorated with [Xml*], with the notable exception of [XmlIgnore], or [DataMember] attributes (and the type’s default constructor) will now be considered as having an implicit [Preserve] attribute.

Please let us know if similar scenarios are not fully working when using the linker.

Advertisements

About spouliot

Xamarin Hacker, Mono Contributor, Open Source Enthusiast with strong interests in Code Analysis, Cryptography and Security. Presently working on MonoTouch, previous projects were Moonlight and libgdiplus and a lot of the cryptographic work done on Mono.
This entry was posted in linker, mono, monotouch, xamarin. Bookmark the permalink.

2 Responses to Linker vs Serialization

  1. Jb Evain says:

    Now that’s pretty cool 🙂

  2. spouliot says:

    Thanks, means a lot coming from “d4 l1nk3r m4n” 🙂

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s