你能强制MonoTouch在其静态编译中包含一个未引用的程序集吗?

时间:2011-04-17 18:15:51

标签: xamarin.ios

我有一个MonoTouch应用程序,可以在运行时动态实例化一个类(使用Type.GetType())。该类位于应用程序中未引用的程序集中,因此MonoTouch静态编译器认为程序集未使用,并在编译应用程序时忽略程序集。如果我在应用程序中添加对类的引用,那么编译器包含程序集,并且对Type.GetType()的调用工作正常:

MyAssembly a;

我更愿意告诉编译器在编译应用程序时始终包含项目“引用”中列出的所有程序集。这可能吗?

谢谢, -Tom B.

2 个答案:

答案 0 :(得分:1)

您必须将项目的链接器行为从“链接所有程序集”更改为“仅限链接SDK程序集”。

另一个解决方案,如果你有用它创建程序集的项目代码,则标记要与PreserveAttribute一起使用的类。

答案 1 :(得分:1)

你能搞清楚吗?如果没有,我遇到了类似的问题:Is there a way to force MonoDevelop to build/load an assembly?

据我了解,这就是C#编译器的工作原理。我能够通过添加一个自定义预构建步骤来解决这个问题,该步骤将类编写到引用程序集中,该程序集包含对未引用程序集的虚拟引用,如下所示:

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);
    }
}