U3D文档中说明了,反射在IOS是支持的,除了system.reflection.emit空间内的,其它都支持。JIT是不支持的。

本质上来说即是:只要不在运行时动态生成代码的行为都支持,reflection.emit下的功能可以动态的生成代码(生成程序集,生成类,生成函数,生成类型等,是真正的生成代码),JIT也能动态的生成代码(如C#生成的IL在各平台上运行时可以由JIT编译成汇编语言)。

其它的并不会生成代码的反射功能是可以用的,如 type.gettype, getfield, Activator.CreateInstance   

文档如下:

Scripting restrictions

 

We strive to provide a common scripting API and experience across all platforms Unity supports. However, some platforms have inherent restrictions. To help you understand these restrictions and support cross-platform code, the following table describes which restrictions apply to each platform and scripting backend:

Platform Scripting Backend Restrictions
Standalone Mono None
WebGL IL2CPP Ahead-of-time compile, No threads
iOS IL2CPP Ahead-of-time compile
Android Mono None
Android IL2CPP Ahead-of-time compile
Samsung TV Mono Ahead-of-time compile
Tizen Mono Ahead-of-time compile
XBox One Mono Ahead-of-time compile
XBox One IL2CPP Ahead-of-time compile
WiiU Mono Ahead-of-time compile
PS Vita Mono Ahead-of-time compile
PS Vita IL2CPP Ahead-of-time compile
PS4 Mono Ahead-of-time compile
PS4 IL2CPP Ahead-of-time compile
Windows Store .NET Uses .NET Core class libraries subset
Windows Store IL2CPP Ahead-of-time compile

Ahead-of-time compile

Some platforms do not allow runtime code generation. Therefore, any managed code which depends upon just-in-time (JIT) compilation on the target device will fail. Instead, we need to compile all of the managed code ahead-of-time (AOT). Often, this distinction doesn’t matter, but in a few specific cases, AOT platforms require additional consideration.

System.Reflection.Emit

An AOT platform cannot implement any of the methods in the System.Reflection.Emit namespace. Note that the rest of System.Reflection is acceptable, as long as the compiler can infer that the code used via reflection needs to exist at runtime.

Serialization

AOT platforms may encounter issues with serialization and deserlization due to the use of reflection. If a type or method is only used via reflection as part of serialization or deserialization, the AOT compiler cannot detect that code needs to be generated for the type or method.

Generic virtual methods

Generic methods require the compiler to do some additional work to expand the code written by the developer to the code actually executed on the device. For example, we need different code for List with an int or a double. In the presence of virtual methods, where behavior is determined at runtime rather than compile time, the compiler can easily require runtime code generation in places where it is not entirely obvious from the source code.

Suppose we have the following code, which works exactly as expected on a JIT platform (it prints “Message value: Zero” to the console once):

using UnityEngine;
using System; public class AOTProblemExample : MonoBehaviour, IReceiver {
public enum AnyEnum {
Zero,
One,
} void Start() {
// Subtle trigger: The type of manager *must* be
// IManager, not Manager, to trigger the AOT problem.
IManager manager = new Manager();
manager.SendMessage(this, AnyEnum.Zero);
} public void OnMessage<T>(T value) {
Debug.LogFormat("Message value: {0}", value);
}
} public class Manager : IManager {
public void SendMessage<T>(IReceiver target, T value) {
target.OnMessage(value);
}
} public interface IReceiver {
void OnMessage<T>(T value);
} public interface IManager {
void SendMessage<T>(IReceiver target, T value);
}

When this code is executed on an AOT platform with the IL2CPP scripting backend, this exception occurs:

ExecutionEngineException: Attempting to call method 'AOTProblemExample::OnMessage<AOTProblemExample+AnyEnum>' for which no ahead of time (AOT) code was generated.
at Manager.SendMessage[T] (IReceiver target, .T value) [0x00000] in <filename unknown>:0
at AOTProblemExample.Start () [0x00000] in <filename unknown>:0

Likewise, the Mono scripting backend provides this similar exception:

  ExecutionEngineException: Attempting to JIT compile method 'Manager:SendMessage<AOTProblemExample/AnyEnum> (IReceiver,AOTProblemExample/AnyEnum)' while running with --aot-only.
at AOTProblemExample.Start () [0x00000] in <filename unknown>:0

The AOT compiler does not realize that it should generate code for the generic method OnMessage with a T of AnyEnum, so it blissfully continues, skipping this method. When that method is called, and the runtime can’t find the proper code to execute, it gives up with this error message.

To work around an AOT issue like this, we can often force the compiler to generate the proper code for us. If we add a method like this to the AOTProblemExample class:

public void UsedOnlyForAOTCodeGeneration() {
// IL2CPP needs only this line.
OnMessage(AnyEnum.Zero); // Mono also needs this line. Note that we are
// calling directly on the Manager, not the IManager interface.
new Manager().SendMessage(null, AnyEnum.Zero); // Include an exception so we can be sure to know if this method is ever called.
throw new InvalidOperationException("This method is used for AOT code generation only. Do not call it at runtime.");
}

When the compiler encounters the explicit call to OnMessage with a T of AnyEnum, it generates the proper code for the runtime to execute. The method UsedOnlyForAOTCodeGeneration does not ever need to be called; it just needs to exist for the compiler to see it.

No threads

Some platforms do not support the use of threads, so any managed code that uses the System.Threading namespace will fail at runtime. Also, some parts of the .NET class libraries implicitly depend upon threads. An often-used example is the System.Timers.Timer class, which depends on support for threads.

U3D开发中关于脚本方面的限制-有关IOS反射和JIT的支持问题的更多相关文章

  1. FPGA开发中的脚本语言

    多数FPGA开发者都习惯图形化界面(GUI).GUI方式简单易学,为小项目提供了一键式流程.然而,随着FPGA项目越来越复杂,在很多情况下GUI工具就阻碍了工作效率.因为GUI工具不能对整个开发过程提 ...

  2. u3d开发中可能会遇到的设计模式

    最近一段时间,面试了一些程序员,当然主要招聘的岗位是Unity3D开发.面试过程中对于三年以上的程序员我都会问其在开发中是否会总结一些常用的设计模式和设计方法,当然目的只是想了解程序员的自我学习情况以 ...

  3. U3D开发性能优化笔记(待增加版本.x)

    http://blog.csdn.net/kaitiren/article/details/45071997 此总结由自己经验及网上收集整理优化内容 包括: .代码方面: .函数使用方面: .ui注意 ...

  4. 解析iOS开发中的FirstResponder第一响应对象

    1. UIResonder 对于C#里所有的控件(例如TextBox),都继承于Control类.而Control类的继承关系如下: 代码如下: System.Object System.Marsha ...

  5. CocoaPoda在iOS开发中的使用

    CocoaPoda在iOS开发中的使用 CocoaPods 简介 CocoaPods是iOS开发中不可避免的依赖管理第三方的工具,能简化一些第三方库文件需要添加编译参数及依赖库的繁复工作 CocoaP ...

  6. gulp自己主动化任务脚本在HybridApp开发中的使用

    眼下做前端开发的同学可能都熟悉grunt.fis之类的自己主动化构建工具.事实上在HybridApp开发中我们也能够使用这些工具来简化我们的工作.gulp就是一个比grunt,fis都先进的构建工具. ...

  7. 老李分享:Eclipse中开发性能测试loadrunner脚本

    老李分享:Eclipse中开发性能测试loadrunner脚本 前篇我分享了如何用loadrunner搭建javauser的性能测试脚本环境,本次我来告诉大家如何在eclipse开发loadrunne ...

  8. VSCode调试Html中的脚本 vscode前端常用插件推荐,搭建JQuery、Vue等开发环境 vsCode 添加浏览器调试和js调试的方法总结 VS Code - Debugger for Chrome调试js

    一.背景 使用Visual Studio Code写了一个简单的Html页面,想调试下其中script标签里的javascript代码,网上查了一通,基本都是复制粘贴或者大同小异的文章,就是要安装De ...

  9. React Native开发中自动打包脚本

    React Native开发中自动打包脚本 在日常的RN开发中,我们避免不了需要将我们编写的代码编译成安装包,然后生成二维码,供需要测试的人员扫描下载.但是对于非原生的开发人员来说,可能不知如何使用X ...

随机推荐

  1. android 照片旋转并保存

    /** * 读取图片属性:旋转的角度 * @param path 图片绝对路径 * @return degree旋转的角度 */ public int readPictureDegree(String ...

  2. 工作T-SQL备忘

    作为一个"浸淫" Oracle 数据库很久的人来说, 突然转入 T-SQL, 也就是 MSSQL , 工作中经常用的查询和 MSMS 使用备忘如下 : --1. 切换对应的库连接 ...

  3. EditPlus 3安装的配置操作

    1.关闭自动保存备份设置 将保存时创建备份文件的钩去掉即可 2.设置字体样式  3.配置编译和运行 运行相当于在控制台执行命令: java -classpath classes Hello 4.设置快 ...

  4. c#数组用法

    随机数: string[]  str = new string[4]{"a","b","c","d"} Readom r ...

  5. mysql视图 触发器 事物 函数 存储过程

    一 视图 视图是一个虚拟表(非真实存在),其本质是[根据SQL语句获取动态的数据集,并为其命名],用户使用时只需使用[名称]即可获取结果集,可以将该结果集当做表来使用. 使用视图我们可以把查询过程中的 ...

  6. 通过表达式树把datareader和datatable转换为实体

    续上两篇文章,使用emit构造dynamic method,把 datareader转换为实体,以避免直接使用反射来实现带来的性能损失.代码看似没有纰漏,但是实际上我在framwork4下运行时,调用 ...

  7. MapReduce源码刨析

    MapReduce编程刨析: Map map函数是对一些独立元素组成的概念列表(如单词计数中每行数据形成的列表)的每一个元素进行指定的操作(如把每行数据拆分成不同单词,并把每个单词计数为1),用户可以 ...

  8. Android购物车的实现,仿淘宝天猫京东等APP。处理RecyclerView或listview中的选中事件;

    很久之前的代码了,拉出来晾晾! 购物车大致思路: 分为:商品.店铺.全选: 商品全部选中后--店铺自动选中:商品未全部选中(若有一个商品未选中)--店铺不选中. 店铺全部选中后--全选自动选中:店铺未 ...

  9. [Unity插件]Lua行为树(八):行为节点扩展

    先看一下之前的行为节点是怎么设计的: BTAction.lua BTAction = BTTask:New(); local this = BTAction; this.taskType = BTTa ...

  10. 公式for TinyMCE 编辑器@ cnblogs.com

    编辑器截图: 行内公式:\(  f(x,y,z) = 3y^2 z \left( 3 + \frac{7x+5}{1 + y^2} \right)  \) 行间公式:\\(  f(x,y,z) = 3 ...