Cross-Platform and Native Applications

Tools like Xamarin and .NET Native enable .NET code to be compiled to target specific platforms. However, given .NET reflection doesn’t necessarily “just work” on all native platforms and auto-wiring of parameters and construction of objects largely relies on reflection, you sometimes have to do some additional work to get Autofac and DI to work.


When using Xamarin to create an iOS or Android app and the linker is enabled, you may need to explicitly describe types requiring reflection. The Xamarin Custom Linker Configuration documentation explains how you can notify the linker to keep certain types and not strip them from the finished product. This boils down to...

  • Mark types you own with a [Preserve] attribute
  • Include a custom XML link description file in your build

A simple link description file looks like this:

  <assembly fullname="mscorlib">
    <type fullname="System.Convert" />
  <assembly fullname="My.Own.Assembly">
    <type fullname="Foo" preserve="fields">
      <method name=".ctor" />
    <namespace fullname="My.Own.Namespace" />
    <type fullname="My.Other*" />

Autofac makes use of the System.Convert.ChangeType method in lambda expressions to convert types so including it in the linker definition is needed. See issue #842 for further discussion.

For additional details on how to structure your Xamarin custom linker configuration file and how to include it in your build, check out the Xamarin documentation.

.NET Native

.NET Native is a way to compile .NET binaries to native code. It’s used in Universal Windows Platform (UWP) and Windows Store apps, among others.

When using .NET Native with reflection you may run into exceptions like MissingMetadataException when the compiler has removed the reflection metadata for types you need.

You can configure .NET Native compilation using a Runtime Directives (rd.xml) file. A simple directive file looks like this:

<Directives xmlns="">
    <Assembly Name="*Application*" Dynamic="Required All" />

That directive file tells the compiler to keep all the reflection data for everything in the entire application package. That’s sort of the “nuclear option” - if you want to make your application package smaller you can be much more specific about what to include. Refer to the MSDN documentation for more detail.