Xamarin.iOS Linker removes members in class needed for reflection


Answers

What you can do is create a file called LinkerPleaseInclude.cs. You can actually name it what you want, but this name makes sense. In there you can create dummy objects for the types you need, especially for reflection. That way the link will see that you "need" it and won't strip it out. Here's an example of a LinkerPleaseInclude.cs file. It's useful if the classes you need are defined in a 3rd party DLL.

Update:

Ah, I misread your original post thinking you were talking about 3rd party DLLs you have no control over. So if you have a non-xamarin.iOS library, you can still use the Preserve attribute. However, you will have to define it like so in your library project.:

public sealed class PreserveAttribute : System.Attribute 
{
    public bool AllMembers;
    public bool Conditional;
}

It doesn't which namespace you define the Preserve class in, as the linker only searches for the Preserve name. Then you can just use the Preserve attribute as you normally would. So if you want to Preserve a class and all 50 properties, then you do something like:

[Preserve(AllMembers=true)]
public MyClass
{
   //code here
}
Question

In a Xamarin project, I set the linker behaviour to "Link all references". When doing that, assemblies get optimized by removing unused members. One problem, it's not actually aware that we need them for reflection. I know there is the Preserve attribute that we can put to tell the linker to keep all symbols in the class, but what if the assembly is a DLL or just not an iOS project (doesn't reference the monotouch assemblies, and can't). Is there a way I can set all classes from a library to "preserve", ignore it, or only set certain classes to be optimized (reverse PreserveAttribute) ?




Links



Tags