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) ?
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
For anyone that uses Reflection to set Properties of UIKit Views here is my "Bread and Butter" Preserve Configuration.
Just create a new class in your Xamarin iOS App project and call it something like "LinkerGuard.cs" or "PreserveConfiguration.cs"
Put this in it:
@SharpMobileCode gave a very good explanation but there are also two other ways to solve this.
Use an XML file and use
--xml=your.file
in the project's options, under Additional mtouch arguments. That might be useful when you can't change the source code of the assemblies -or- when you want/need to use a tool to generate the list of members to be preserved;Use the new
Preserve(Type)
constructor. That allows you to add preserve instructions in a different assembly (e.g. your main .exe) which already has a reference toXamarin.iOS.dll
(so no need to define your own type). E.g.[assembly: Preserve (typeof (MyType), AllMembers = true)]
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.:
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: