防止Xamarin(/Mono?)在iOS上进行优化 [英] Preventing Xamarin(/Mono?) from optimizing on iOS

查看:136
本文介绍了防止Xamarin(/Mono?)在iOS上进行优化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我像这样在ViewDidLoad中使用反射时:

When I use reflection in ViewDidLoad like this:

foreach (var m in this.GetType().GetMethods()) {
            Console.WriteLine (m.Name); 
}

没有任何其他代码,例如

without any other code, methods like

Add
set_View

不在那里;当我添加;

are not there; when I add;

public void PreventOptimizing() {
    var x = this.View; 
    this.View = x; 
    this.Add (null); 
}

到该类,并且没有调用该方法,它们在那里.因此,我假设AOT编译可以优化这些方法,因为它们不会被调用.我不知道它添加了哪些方法,因此我希望编译器在我的实验中不要优化任何东西.那怎么办?还是有另一种技巧可以阻止自动删除方法?

to the class and without calling that method, they are there. So I assume the AOT compilation optimizes these methods away as they are not called. I don't know which methods it adds away so I would like the compiler, for my experiment, not to optimize anything away. How can that be done? Or is there another trick preventing automated removal of method?

完整的代码,如果还不够清楚的话;

so full code, if this isn't clear enough;

  1. 结果输出将不包含"Add"和"set_View";

public class TestController : UIViewController
{

    public override void ViewDidLoad ()
  {
         base.ViewDidLoad ();

      foreach (var m in this.GetType().GetMethods()) {
          Console.WriteLine (m.Name); 
      }
    }
}

  1. 输出中确实包含Add和set_View;

public class TestController : UIViewController
{
  public void PreventOptimizing() {
      var x = this.View; 
      this.View = x; 
      this.Add (null); 
  }
  public override void ViewDidLoad ()
  {
         base.ViewDidLoad ();

      foreach (var m in this.GetType().GetMethods()) {
          Console.WriteLine (m.Name); 
      }
    }
}

推荐答案

您可以将链接器选项设置为仅链接SDK程序集",或使用

You either set the linker option to "Link SDK assemblies only" or use the PreserveAttribute on the class.

详细信息在Xamarin网站上: http://developer.xamarin.com/Guides/ios/Advanced_Topics/Linker/

Details are in Xamarin website: http://developer.xamarin.com/Guides/ios/Advanced_Topics/Linker/

链接器行为

可以用不同的方式自定义链接过程.首要的 Xamarin Studio中用于控制链接器的机制是链接器 iOS Build Xamarin Studio的项目"选项中的行为"下拉列表 对话框.在Visual Studio中,它位于项目属性"中, 在iOS Build下.

The linking process can be customized in different ways. The primary mechanism for controlling the linker in Xamarin Studio is the Linker Behavior drop-down in the iOS Build Xamarin Studio's Project Options dialog box. In Visual Studio, this is located in project Properties, under iOS Build.

保存代码

使用链接器时,有时可以删除代码 您可能已经使用以下方法动态调用过 System.Reflection.MemberInfo.Invoke,或将您的方法导出到 使用[Export]属性的Objective-C,然后调用 手动选择器.

When you use the linker it can sometimes remove code that you might have called dynamically either using System.Reflection.MemberInfo.Invoke, or by exporting your methods to Objective-C using the [Export] attribute and then invoking the selector manually.

在这种情况下,您可以指示链接器考虑整个 通过申请使用的类或要保留的个人成员 [Xamarin.iOS.Foundation.Preserve]属性位于 类级别或成员级别.每个不是静态的成员 应用程序链接的内容可能会被删除.该属性是 因此用于标记未静态引用的成员,但 您的应用程序仍然需要的.

In those cases, you can instruct the linker to consider either entire classes to be used or individual members to be preserved by applying the [Xamarin.iOS.Foundation.Preserve] attribute either at the class-level or the member-level. Every member that is not statically linked by the application is subject to be removed. This attribute is hence used to mark members that are not statically referenced, but that are still needed by your application.

例如,如果您动态实例化类型,则可能需要 保留类型的默认构造函数.如果您使用XML 序列化,您可能想要保留类型的属性.

For instance, if you instantiate types dynamically, you may want to preserve the default constructor of your types. If you use XML serialization, you may want to preserve the properties of your types.

您可以将此属性应用于类型的每个成员或类型 本身.如果要保留整个类型,可以使用以下语法 在类型上 [保留(AllMembers = true)] .

You can apply this attribute on every member of a type, or on the type itself. If you want to preserve the whole type, you can use the syntax [Preserve (AllMembers = true)] on the type.

有时您想保留某些成员,但前提是 包含类型被保留.在这种情况下,请使用[保留 (Conditional = true)]

Sometimes you want to preserve certain members, but only if the containing type was preserved. In those cases, use [Preserve (Conditional=true)]

这篇关于防止Xamarin(/Mono?)在iOS上进行优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆