Can you force MonoTouch to include an unreferenced

2019-07-11 08:50发布

问题:

I have a MonoTouch app that dynamically instantiates a class (using Type.GetType()) at runtime. The class is in an assembly that is not referenced anywhere else in the app, so the MonoTouch static compiler thinks that the assembly isn't used and ignores the assembly when it compiles the app. If I add a reference to the class in the app, then the compiler includes the assembly and the call to Type.GetType() works fine:

MyAssembly a;

I would prefer to just tell the compiler to always include all the assemblies listed in the project's "References" when it compiles the app. Is this possible?

Thanks, -Tom B.

回答1:

You will have to change your project's Linker behavior from "Link all assemblies" to "Link SDK assemblies only".

The other solution, if you have the project code that assembly was created with, is to mark the class you want to use with the PreserveAttribute.



回答2:

Were you able to figure this out yet? If not, I had a similar problem: Is there a way to force MonoDevelop to build/load an assembly?

As I understand it, that's just how the C# compiler works. I was able to get around this by adding a custom pre-build step that scripts a class into the referencing assembly that includes dummy references to the unreferenced assemblies, like so:

using System;

namespace MyNamespace
{
    public static class Referencer
    {
        Type t;

        //These lines are scripted one per class in the unreferenced assemblies
        //You should only need one per assembly, but I don't think more hurts.
        t = typeof(Namespace1.Class1);
        t = typeof(Namespace2.Class2);
        ...
        t = typeof(NamespaceN.ClassN);
    }
}


标签: xamarin.ios