Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the meaning of a LinkerPleaseInclude.cs file in a Xamarin application or library?

In MvvmCross library there is a class Playground.Droid.LinkerPleaseInclude.cs https://github.com/MvvmCross/MvvmCross/blob/develop/ContentFiles/Android/LinkerPleaseInclude.cs

At line 103 there is a method:

public void Include(MvxTaskBasedBindingContext context)
{
  context.Dispose();
  var context2 = new MvxTaskBasedBindingContext();
  context2.Dispose();
}

Inside this method it is created an instance of MvxTaskBasedBindingContext class and then immediately it is disposed. Question: Why it is necessary here to create this instance and after to dispose it?

like image 802
Melnic Vadim Avatar asked Dec 02 '25 10:12

Melnic Vadim


1 Answers

The LinkerPleaseInclude.cs is a concept used to prevent Mono's linker from stripping these types from your application. You can read more about this here:

https://github.com/mono/linker/blob/master/src/linker/README.md

What the code you're seeing does prevents these types from being marked and swept by the linker so they are "included" in your application even after the linker strips IL code.

It uses static analysis of your application to determine what assemblies, types, and members are used. If it does not detect these items to be valid, they are marked and swept.

https://learn.microsoft.com/en-us/xamarin/android/deploy-test/linker

like image 139
Jon Douglas Avatar answered Dec 04 '25 22:12

Jon Douglas