Runar Ovesen Hjerpbakk

Programmer. Software Architect. Technical Manager.

Unhandled managed exception - Default constructor not found

The Xamarin.iOS linker can be your best friend or worst enemy. It removes unused classes, methods or properties being from the DLLs in your app, thus decreasing its size. The downside is that you need to manually include code only used by reflection, serialization or other techniques hiding the code from the static analysis the linker uses. As the linker runs at AOT compile time, code only visible during runtime is skipped.

As I’ve written before, it’s easy to adjust the linker behavior, but what to do when these adjustments are not enough?

The bug

I use a cross-platform CardView in my new app. The control works great and I’ve instructed the linker to skip its libraries:

One day this stopped working and the app started crashing with a SIGABRT: Default constructor not found for type PanCardView.Controls.IndicatorItemView. But, and here’s the funny part, only in Release mode and only on the iPhone XR simulator. The settings were the same for all configurations and all simulators, but in release on the iPhone XR simulator in crashed and for all others it worked.

The error message was pretty adamant the linker was to blame.

Unhandled managed exception: Default constructor not found for type PanCardView.Controls.IndicatorItemView (System.MissingMethodException)

But why the exception only occurred in one configuration and worked when identically configured in the others are beyond me.

The solution

Pragmatic as I am though, even this mysterious error is easy to “fix”. I just added a call to this constructor directly, thus preventing the linker from removing the constructor.

// Hack to preserve constructor
var notUsed = new PanCardView.Controls.IndicatorItemView();