2

Apologies if this sounds like a silly question. I'm not very experienced with how things are linked/bundled/assembled under the hood.

Before I begin, I'd like to say that I've tried reading documentation (such as https://learn.microsoft.com/en-us/xamarin/cross-platform/app-fundamentals/code-sharing) to find the answer, but was unable to.

If I have a Xamarin.Forms solution and I reference a .Net Standard project:

Question 1: Does all the code from this project get compiled and included into the app such that it may be disassembled later, or is it only code from classes that I actually make use of that gets included?

Bit more elaboration:
For example, I may have a School class that expects an IStudent (inject via DI), and a Student class that implements IStudent. Both of these exist in the .Net Standard project that I reference in the Xamarin.Forms project. However, if I only actually make use of the Student class (by registering it with type IStudent in my IoC container), will the code from School get included in the built app as well?


Question 2: If all the code from the project does get included, is there a way to forcefully specify which classes to include/exclude by way of some configuration setting, attributes, 3rd-party library, or something else?

1 Answer 1

1

As far as i know everything in the NETStandard project get compiled and shipped with the app.

If you want to remove unused code from compiled assemblies you have to use the linker. To link everything, you have to select "Sdk and User Assemblies".

The linker tries to dont strip away mthods and fields you are using, but often is too aggressive (for example, methods referenced only by reflection will be stripped).

Luckily there are few methods where you can fine-tune the linker behaviour and make it work. Some link to elaborate on:



2
  • Thank you for that information. So if I set the Linker to link all in my iOS and Android projects, classes from the .Net Standard project (that is referenced by my Forms project) that are not actually used, will be stripped away and not included in the final assembly? Is my understanding right? On a side note, terminology wise, it seems quite counter-intuitive to me that if I don't want code linked (and therefore included) I have to to use 'linking' :-)
    – AjLearning
    Jun 15, 2019 at 9:45
  • yes, your understanding is right: all your unused code will be stripped away from the final assemblies. The name "linker" is because it's part of the linking process. The linking process brings together dependencies to produce the final product ( an assembly in the .net world ). The mono linker links the bare minimum resources. This is a common usage of the term. See the JVM linker for example, which performs similar tasks. Jun 20, 2019 at 10:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.