因为 Unity 对 Mono 的版权问题,当前 Unity 中集成的 Mono 是相对比较旧的版本,而目前的 Unity 版本已经全面支持 il2cpp 技术了,相对于 Mono,il2cpp 是 Unity 官方更推荐的一个方案,il2cpp 将 C# 代码转换为 C++ 代码,转换之后的 C++ 代码必须完全兼容 C# 的反射机制,所以运行时需要加载完整的反射数据,而反射数据占用了相当大的内存空间。
1. il2cpp 转换程序
il2cpp 代码转换依赖于il2cpp.exe程序,它是一个 AOT 编译工具,放在 Unity 的安装目录下,路径在C:\Program Files\Unity_2018_4_13_f1\Editor\Data\il2cpp\build
下,Unity 在构建时,先将 C# 源代码生成 .dll 动态库(IL中间语言),然后调用 il2cpp.exe 将 IL 转换为 C++ 源代码。自定义脚本生成的 C++ 代码文件命名通常是 Bulk_Assembly_CSharp_0.cpp
,随着脚本数量增加,会生成更多的文件,文件名中的数字递增。
2. 转换规则
2.1 枚举类型
在 C# 代码中声明一个枚举类型的代码
public enum EntityType
{
Player = 0,
Monster = 1,
Partner = 2
}
生成的 C++ 源码中和 EntityType 相关的代码有
#ifndef ENTITYTYPE_TD7959D0FFE8516191628FDC104DD7B98D64362A7_H
#define ENTITYTYPE_TD7959D0FFE8516191628FDC104DD7B98D64362A7_H
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winvalid-offsetof"
#pragma clang diagnostic ignored "-Wunused-variable"
#endif
// EntityType
struct EntityType_tD7959D0FFE8516191628FDC104DD7B98D64362A7
{
public:
// System.Int32 EntityType::value__
int32_t ___value___2;
public:
inline static int32_t get_offset_of_value___2() { return static_cast(offsetof(EntityType_tD7959D0FFE8516191628FDC104DD7B98D64362A7, ___value___2)); }
inline int32_t get_value___2() const { return ___value___2; }
inline int32_t* get_address_of_value___2() { return &___value___2; }
inline void set_value___2(int32_t value)
{
___value___2 = value;
}
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif // ENTITYTYPE_TD7959D0FFE8516191628FDC104DD7B98D64362A7_H
可以看到,枚举类型是生成了一个包含整型变量的 struct 结构类型,有一个变量 __value__2,且对应的生成了 4 个内联方法
2.2 接口
C# 接口定义代码如下
interface IRunable
{
void Run();
}
搜索生成的C++代码,没有 IRunable 关键字,可以知道没有被实现的接口不会生成任何的 C++ 代码。
被实现的接口,会生成 RuntimeClass* 类型的指针:
extern RuntimeClass* IRunable_tD667EDDE8FDF251385C0934B011CBCC07BCCAF9F_il2cpp_TypeInfo_var;
并且实现该接口的类型会把该实现当成一个成员函数来生成相关数据。
2.3 结构体
在 C# 代码中定义如下的结构体
public struct EntityData
{
public int configId;
public string entityDesc;
public int attackValue;
public static string KEY_DATA_CONFIG = "Entity_Data";
public int GetEntityAttackValue()
{
return this.attackValue;
}
}
转换得到的相关 C++ 代码包括
#ifndef ENTITYDATA_T1FE2E889F592CFB2CFF47B578B4B09C4D5980503_H
#define ENTITYDATA_T1FE2E889F592CFB2CFF47B578B4B09C4D5980503_H
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winvalid-offsetof"
#pragma clang diagnostic ignored "-Wunused-variable"
#endif
// EntityData
struct EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503
{
public:
// System.Int32 EntityData::configId
int32_t ___configId_0;
// System.String EntityData::entityDesc
String_t* ___entityDesc_1;
// System.Int32 EntityData::attackValue
int32_t ___attackValue_2;
public:
inline static int32_t get_offset_of_configId_0() { return static_cast(offsetof(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503, ___configId_0)); }
inline int32_t get_configId_0() const { return ___configId_0; }
inline int32_t* get_address_of_configId_0() { return &___configId_0; }
inline void set_configId_0(int32_t value)
{
___configId_0 = value;
}
inline static int32_t get_offset_of_entityDesc_1() { return static_cast(offsetof(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503, ___entityDesc_1)); }
inline String_t* get_entityDesc_1() const { return ___entityDesc_1; }
inline String_t** get_address_of_entityDesc_1() { return &___entityDesc_1; }
inline void set_entityDesc_1(String_t* value)
{
___entityDesc_1 = value;
Il2CppCodeGenWriteBarrier((&___entityDesc_1), value);
}
inline static int32_t get_offset_of_attackValue_2() { return static_cast(offsetof(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503, ___attackValue_2)); }
inline int32_t get_attackValue_2() const { return ___attackValue_2; }
inline int32_t* get_address_of_attackValue_2() { return &___attackValue_2; }
inline void set_attackValue_2(int32_t value)
{
___attackValue_2 = value;
}
};
struct EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_StaticFields
{
public:
// System.String EntityData::KEY_DATA_CONFIG
String_t* ___KEY_DATA_CONFIG_3;
public:
inline static int32_t get_offset_of_KEY_DATA_CONFIG_3() { return static_cast(offsetof(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_StaticFields, ___KEY_DATA_CONFIG_3)); }
inline String_t* get_KEY_DATA_CONFIG_3() const { return ___KEY_DATA_CONFIG_3; }
inline String_t** get_address_of_KEY_DATA_CONFIG_3() { return &___KEY_DATA_CONFIG_3; }
inline void set_KEY_DATA_CONFIG_3(String_t* value)
{
___KEY_DATA_CONFIG_3 = value;
Il2CppCodeGenWriteBarrier((&___KEY_DATA_CONFIG_3), value);
}
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif
// Native definition for P/Invoke marshalling of EntityData
struct EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshaled_pinvoke
{
int32_t ___configId_0;
char* ___entityDesc_1;
int32_t ___attackValue_2;
};
// Native definition for COM marshalling of EntityData
struct EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshaled_com
{
int32_t ___configId_0;
Il2CppChar* ___entityDesc_1;
int32_t ___attackValue_2;
};
#endif // ENTITYDATA_T1FE2E889F592CFB2CFF47B578B4B09C4D5980503_H
仔细看生成的 C++ 代码,可以知道 C# 的结构体转换后的 C++ 代码包括:
2.3.1 对应一个 C++ 的结构体定义
- 生成 RuntimeClass* 指针
extern RuntimeClass* EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_il2cpp_TypeInfo_var;
- 结构体包含C#结构体中的所有成员,类型的映射是:
int -> System.Int32 -> int32_t
string -> System.String -> String_t* - 针对每一个成员变量,生成四个内联函数
get_offset_of_xxxx
get_xxxxx
set_xxxxx
get_address_of_xxxx
2.3.2 将静态成员单独封装到一个结构体中
罗列所有静态成员,每个静态成员生成四件套方法,且setter方法中调用了 Il2CppCodeGenWriteBarrier
方法
2.3.3 针对 marshaled_pinvoke 的一个结构体定义
罗列成员变量,string 类型对应 char* 类型
2.3.4 针对 marshaled_COM 的一个结构体定义
罗列成员变量,string 类型对应 Il2CppChar* 类型
2.3.4 生成结构体的成员方法相关
- 方法声明
// System.Int32 EntityData::GetEntityAttackValue()
extern "C" IL2CPP_METHOD_ATTR int32_t EntityData_GetEntityAttackValue_m4290D9B0FEB9D142452D6491098183E999CF7040 (EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 * __this, const RuntimeMethod* method);
- 方法实现
// System.Int32 EntityData::GetEntityAttackValue()
extern "C" IL2CPP_METHOD_ATTR int32_t EntityData_GetEntityAttackValue_m4290D9B0FEB9D142452D6491098183E999CF7040 (EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 * __this, const RuntimeMethod* method)
{
{
int32_t L_0 = __this->get_attackValue_2();
return L_0;
}
}
- AdjustorTrunk 的方法实现
extern "C" int32_t EntityData_GetEntityAttackValue_m4290D9B0FEB9D142452D6491098183E999CF7040_AdjustorThunk (RuntimeObject * __this, const RuntimeMethod* method)
{
EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 * _thisAdjusted = reinterpret_cast(__this + 1);
return EntityData_GetEntityAttackValue_m4290D9B0FEB9D142452D6491098183E999CF7040(_thisAdjusted, method);
}
2.3.5 和 marshal 相关的转换方法
// Conversion methods for marshalling of: EntityData
extern "C" void EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshal_pinvoke(const EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503& unmarshaled, EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshaled_pinvoke& marshaled)
{
marshaled.___configId_0 = unmarshaled.get_configId_0();
marshaled.___entityDesc_1 = il2cpp_codegen_marshal_string(unmarshaled.get_entityDesc_1());
marshaled.___attackValue_2 = unmarshaled.get_attackValue_2();
}
extern "C" void EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshal_pinvoke_back(const EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshaled_pinvoke& marshaled, EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503& unmarshaled)
{
int32_t unmarshaled_configId_temp_0 = 0;
unmarshaled_configId_temp_0 = marshaled.___configId_0;
unmarshaled.set_configId_0(unmarshaled_configId_temp_0);
unmarshaled.set_entityDesc_1(il2cpp_codegen_marshal_string_result(marshaled.___entityDesc_1));
int32_t unmarshaled_attackValue_temp_2 = 0;
unmarshaled_attackValue_temp_2 = marshaled.___attackValue_2;
unmarshaled.set_attackValue_2(unmarshaled_attackValue_temp_2);
}
// Conversion method for clean up from marshalling of: EntityData
extern "C" void EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshal_pinvoke_cleanup(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshaled_pinvoke& marshaled)
{
il2cpp_codegen_marshal_free(marshaled.___entityDesc_1);
marshaled.___entityDesc_1 = NULL;
}
// Conversion methods for marshalling of: EntityData
extern "C" void EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshal_com(const EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503& unmarshaled, EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshaled_com& marshaled)
{
marshaled.___configId_0 = unmarshaled.get_configId_0();
marshaled.___entityDesc_1 = il2cpp_codegen_marshal_bstring(unmarshaled.get_entityDesc_1());
marshaled.___attackValue_2 = unmarshaled.get_attackValue_2();
}
extern "C" void EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshal_com_back(const EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshaled_com& marshaled, EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503& unmarshaled)
{
int32_t unmarshaled_configId_temp_0 = 0;
unmarshaled_configId_temp_0 = marshaled.___configId_0;
unmarshaled.set_configId_0(unmarshaled_configId_temp_0);
unmarshaled.set_entityDesc_1(il2cpp_codegen_marshal_bstring_result(marshaled.___entityDesc_1));
int32_t unmarshaled_attackValue_temp_2 = 0;
unmarshaled_attackValue_temp_2 = marshaled.___attackValue_2;
unmarshaled.set_attackValue_2(unmarshaled_attackValue_temp_2);
}
// Conversion method for clean up from marshalling of: EntityData
extern "C" void EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshal_com_cleanup(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_marshaled_com& marshaled)
{
il2cpp_codegen_marshal_free_bstring(marshaled.___entityDesc_1);
marshaled.___entityDesc_1 = NULL;
}
2.3.6 cctor 方法
生一个普通的 C++ 方法作为成静态成员构造方法
// System.Void EntityData::.cctor()
extern "C" IL2CPP_METHOD_ATTR void EntityData__cctor_m120084307B057CEE137CF568D34F6605ABFA1A78 (const RuntimeMethod* method)
{
static bool s_Il2CppMethodInitialized;
if (!s_Il2CppMethodInitialized)
{
il2cpp_codegen_initialize_method (EntityData__cctor_m120084307B057CEE137CF568D34F6605ABFA1A78_MetadataUsageId);
s_Il2CppMethodInitialized = true;
}
{
((EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_StaticFields*)il2cpp_codegen_static_fields_for(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503_il2cpp_TypeInfo_var))->set_KEY_DATA_CONFIG_3(_stringLiteral970DB5A495B5795A0B46F6CD429575DC72AE6AA6);
return;
}
}
2.4 普通类
为了比较全面的测试类的代码转换,这里定义的类包括了普通的私有成员变量、属性、实例方法、静态方法,并考虑了类的派生,最终得到的测试代码如下,首先是基类代码,包含了四种类型的成员变量,一个属性,两个成员方法和一个静态方法
public class BaseEntity
{
private int _entityId;
public string entityName;
public EntityType entityType;
public EntityData entityData;
public int EntityId
{
get
{
return this._entityId;
}
set
{
this._entityId = value;
}
}
private void Start()
{
this.entityData = new EntityData();
this.entityData.attackValue = 100;
}
public void Attack()
{
Debug.Log("BaseEntity.Attack");
}
public static string GetEntityDesc()
{
return "entity";
}
}
生成的C++相关代码包括以下几部分
2.4.1 相关结构体声明
在生成的 C++ 文件最前面有关于 C# 中 class 转换成的 C++ 的 struct 的相关声明
// BaseEntity
struct BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC;
生成 RuntimeClass* 指针
extern RuntimeClass* BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC_il2cpp_TypeInfo_var;
2.4.2 MetadataUsageId
在 C++ 文件开头部分,会为每一个成员方法声明一个 uint32_t 类型的 id
extern const uint32_t BaseEntity_Attack_m38372EF5D100F37FA07CE65CDF09A67122356F06_MetadataUsageId;
extern const uint32_t BaseEntity_GetEntityDesc_m9044C9F37CA9BE69015A560E75854C1B16DD8471_MetadataUsageId;
2.4.3 对应的结构体声明
生成的 C++ 的 struct 如下:
// BaseEntity
struct BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC : public RuntimeObject
{
public:
// System.Int32 BaseEntity::_entityId
int32_t ____entityId_0;
// System.String BaseEntity::entityName
String_t* ___entityName_1;
// EntityType BaseEntity::entityType
int32_t ___entityType_2;
// EntityData BaseEntity::entityData
EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 ___entityData_3;
public:
inline static int32_t get_offset_of__entityId_0() { return static_cast(offsetof(BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC, ____entityId_0)); }
inline int32_t get__entityId_0() const { return ____entityId_0; }
inline int32_t* get_address_of__entityId_0() { return &____entityId_0; }
inline void set__entityId_0(int32_t value)
{
____entityId_0 = value;
}
inline static int32_t get_offset_of_entityName_1() { return static_cast(offsetof(BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC, ___entityName_1)); }
inline String_t* get_entityName_1() const { return ___entityName_1; }
inline String_t** get_address_of_entityName_1() { return &___entityName_1; }
inline void set_entityName_1(String_t* value)
{
___entityName_1 = value;
Il2CppCodeGenWriteBarrier((&___entityName_1), value);
}
inline static int32_t get_offset_of_entityType_2() { return static_cast(offsetof(BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC, ___entityType_2)); }
inline int32_t get_entityType_2() const { return ___entityType_2; }
inline int32_t* get_address_of_entityType_2() { return &___entityType_2; }
inline void set_entityType_2(int32_t value)
{
___entityType_2 = value;
}
inline static int32_t get_offset_of_entityData_3() { return static_cast(offsetof(BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC, ___entityData_3)); }
inline EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 get_entityData_3() const { return ___entityData_3; }
inline EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 * get_address_of_entityData_3() { return &___entityData_3; }
inline void set_entityData_3(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 value)
{
___entityData_3 = value;
}
};
struct BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC_StaticFields
{
public:
// System.String BaseEntity::ENTITY_KEY
String_t* ___ENTITY_KEY_4;
public:
inline static int32_t get_offset_of_ENTITY_KEY_4() { return static_cast(offsetof(BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC_StaticFields, ___ENTITY_KEY_4)); }
inline String_t* get_ENTITY_KEY_4() const { return ___ENTITY_KEY_4; }
inline String_t** get_address_of_ENTITY_KEY_4() { return &___ENTITY_KEY_4; }
inline void set_ENTITY_KEY_4(String_t* value)
{
___ENTITY_KEY_4 = value;
Il2CppCodeGenWriteBarrier((&___ENTITY_KEY_4), value);
}
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif // BASEENTITY_T10F88AF9625E7158E20269E2A8CEA034B0C8EEFC_H
几个关键点
- 直接或间接从
RuntimeObject
派生
包括MonoBehaviour
,Component
,Object
等都会生成对应的 C++ 结构体,最上层的Object
从RuntimeObject
派生 - 依然是罗列每个成员变量,并且为每个成员变量生成 4 个内联方法
- 静态成员单独封装成 _StaticFields 的结构体,并生成
_cctor
构造方法
/ System.Void BaseEntity::.cctor()
extern "C" IL2CPP_METHOD_ATTR void BaseEntity__cctor_mF8F13F2BA7F8EADA6B2168388956ACE469918973 (const RuntimeMethod* method)
{
static bool s_Il2CppMethodInitialized;
if (!s_Il2CppMethodInitialized)
{
il2cpp_codegen_initialize_method (BaseEntity__cctor_mF8F13F2BA7F8EADA6B2168388956ACE469918973_MetadataUsageId);
s_Il2CppMethodInitialized = true;
}
{
((BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC_StaticFields*)il2cpp_codegen_static_fields_for(BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC_il2cpp_TypeInfo_var))->set_ENTITY_KEY_4(_stringLiteralF5EE4FB0250368081F8F299B9158F95FC69D5BA8);
return;
}
}
- 会生成带
_ctor
的构造方法
// System.Void BaseEntity::.ctor()
extern "C" IL2CPP_METHOD_ATTR void BaseEntity__ctor_m2555EEA5D80679F2A34DA92DC575088A37BCE2B9 (BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC * __this, const RuntimeMethod* method)
{
{
Object__ctor_m925ECA5E85CA100E3FB86A4F9E15C120E9A184C0(__this, /*hidden argument*/NULL);
return;
}
}
- 不会生成关于 marshaled_pinvoke 和 marshaled_COM 的成员方法和转换方法
- 每个属性都会当成
get_XXXX
和set_XXXX
的成员方法来处理
// System.Int32 BaseEntity::get_EntityId()
extern "C" IL2CPP_METHOD_ATTR int32_t BaseEntity_get_EntityId_mDA09A2A9BC119AC5E696B27FFD4AC166AEF9643D (BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC * __this, const RuntimeMethod* method)
{
{
int32_t L_0 = __this->get__entityId_0();
return L_0;
}
}
// System.Void BaseEntity::set_EntityId(System.Int32)
extern "C" IL2CPP_METHOD_ATTR void BaseEntity_set_EntityId_m40F270BE010D0145AFF939EE09050742AFA369C9 (BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC * __this, int32_t ___value0, const RuntimeMethod* method)
{
{
int32_t L_0 = ___value0;
__this->set__entityId_0(L_0);
return;
}
}
- 成员方法和静态方法都会生成 MetadataUsageId 和对应的方法实现,区别是静态方法的参数中没有
this
参数
// System.Void BaseEntity::Attack()
extern "C" IL2CPP_METHOD_ATTR void BaseEntity_Attack_m38372EF5D100F37FA07CE65CDF09A67122356F06 (BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC * __this, const RuntimeMethod* method)
{
static bool s_Il2CppMethodInitialized;
if (!s_Il2CppMethodInitialized)
{
il2cpp_codegen_initialize_method (BaseEntity_Attack_m38372EF5D100F37FA07CE65CDF09A67122356F06_MetadataUsageId);
s_Il2CppMethodInitialized = true;
}
{
IL2CPP_RUNTIME_CLASS_INIT(Debug_t7B5FCB117E2FD63B6838BC52821B252E2BFB61C4_il2cpp_TypeInfo_var);
Debug_Log_m4B7C70BAFD477C6BDB59C88A0934F0B018D03708(_stringLiteral3111252610AB899A829E540D65AF641ADD2F1633, /*hidden argument*/NULL);
return;
}
}
// System.String BaseEntity::GetEntityDesc()
extern "C" IL2CPP_METHOD_ATTR String_t* BaseEntity_GetEntityDesc_m9044C9F37CA9BE69015A560E75854C1B16DD8471 (const RuntimeMethod* method)
{
static bool s_Il2CppMethodInitialized;
if (!s_Il2CppMethodInitialized)
{
il2cpp_codegen_initialize_method (BaseEntity_GetEntityDesc_m9044C9F37CA9BE69015A560E75854C1B16DD8471_MetadataUsageId);
s_Il2CppMethodInitialized = true;
}
{
return _stringLiteralE711631380EF1B422AE392DB3CA08B8E061AEA4E;
}
}
- 虚方法
会为子类和父类分别生成一份虚方法(包括 MetadataUsageId 和 IL2Cpp_Method_Attr修饰的实现),在调用时通过VirtActionInvoke
来调用
3. 总结
3.1 对于 struct 和 class 生成的代码对比总结如下
3.2 重点关注 C# 中 class 转换规则
3.2.0 struct 声明
在生成的 C++ 代码的开头,会生成对应的 struct 声明:
// BaseEntity
struct BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC;
3.2.1 从 RuntimeObject 派生的 struct
所有 class 都转换成 C++ 的 struct,直接或间接派生自 RuntimeObject,如果 class 从 MonoBehaviour 派生,则生成的 struct 派生链如下
3.2.2 为每个成员变量提供四件套方法
转换后的 struct 包含 class 中所有的成员变量,并为每一个成员变量生成四件套方法,包括
- 获取变量距离对象首地址的偏移值
inline static int32_t get_offset_of_configId_0() { return static_cast(offsetof(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503, ___configId_0)); }
- 获取变量地址
inline int32_t* get_address_of_configId_0() { return &___configId_0; }
- getter 方法
inline int32_t get_configId_0() const { return ___configId_0; }
- setter 方法
inline void set_configId_0(int32_t value)
{
___configId_0 = value;
}
3.2.3 所有静态成员被封装成额外的结构体 _StaticFIelds
该结构体罗列所有静态成员,并为每个成员生成对应的四件套方法,四件套方法中的 setter 方法会调用额外的 il2cpp 相关方法Il2CppCodeGenWriteBarrier
进行处理
inline void set_KEY_DATA_CONFIG_3(String_t* value)
{
___KEY_DATA_CONFIG_3 = value;
Il2CppCodeGenWriteBarrier((&___KEY_DATA_CONFIG_3), value);
}
3.2.4 class 的方法被转换为普通的 C 语言函数
在方法调用时传入对象实例即可,静态成员函数不需要提供对象实例参数,C# 中 class 的属性,会对应生成 get_XXX 和 set_XXX 的 C 语言函数。方法的实现都有 IL2CPP_METHOD_ATTR
修饰符。
- MetadataUsageId
每个方法都会生成一个 metadataUsagedId,包括成员方法、静态方法和 _ctor,_cctor 方法都会生成。实际测试结果,class 的属性生成的方法,不会生成 metadataUsageId。
3.2.5不会生成 marshaled 相关的结构体和方法
C# 中的 struct 转换成 C++ 的 struct 后,会生成对应的 marshaled 相关的结构体和转换、逆转换、清除方法,单 class 转换后不会生成。
3.2.6 会生成构造方法 _ctor
转换后会生成带 _ctor 的 C 语言构造函数,_StaticFields 会生成 _cctor 构造函数。
3.2.7 关于RuntimeObject
查看 il2cpp 的源码,可以找到 RuntimeObject
的定义如下:
typedef Il2CppObject RuntimeObject;
再看 Il2CppObject
的定义如下:
typedef struct Il2CppObject
{
union
{
Il2CppClass *klass;
Il2CppVTable *vtable;
};
MonitorData *monitor;
} Il2CppObject;
也就是说,C# 中 class 的每一个对象,实际上天然就带了两个指针。
4. 运行时
4.1 class 对象创建
在 C# 创建对象的代码大概是如下:
_instance = new EntityUtil();
转换后的对象创建关键代码是
EntityUtil_t2E2847E9DC2DF017EC8CA57209133CD7BECBA9D9 * L_1 = (EntityUtil_t2E2847E9DC2DF017EC8CA57209133CD7BECBA9D9 *)il2cpp_codegen_object_new(EntityUtil_t2E2847E9DC2DF017EC8CA57209133CD7BECBA9D9_il2cpp_TypeInfo_var);
核心函数为 il2cpp 的函数 il2cpp_codegen_object_new
, 在 il2cpp 源码中查看,它的实现是
inline RuntimeObject* il2cpp_codegen_object_new(RuntimeClass *klass)
{
return il2cpp::vm::Object::New(klass);
}
进一步可以在 il2cpp 目录的 Object.cpp 中看到具体实现
Il2CppObject* Object::New(Il2CppClass *klass)
{
// same as NewAllocSpecific as we only support a single domain
return NewAllocSpecific(klass);
}
NewAllocSpecific
方法会根据 klass.instance_size ,在il2cpp内存池中分配一个内存空间,然后调用对应的构造函数初始化,这里列出核心代码,完整实现可以查看 il2cpp\libil2cpp\vm\Object.cpp
if (!klass->has_references)
{
o = NewPtrFree(klass);
}
else
{
o = Allocate(klass->instance_size, klass);
}
if (klass->has_finalize)
il2cpp::gc::GarbageCollector::RegisterFinalizerForNewObject(o);
Runtime::ClassInit(klass);
return o;
4.2 struct 对象创建
C# 中创建 struct 对象的测试代码如下
this.entityData = new EntityData();
this.entityData.attackValue = 100;
生成的 C++ 代码要简单得多,在栈上定义一个 struct 局部变量,在作用域结束后终止生命周期。
EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 * L_0 = __this->get_address_of_entityData_3();
il2cpp_codegen_initobj(L_0, sizeof(EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 ));
EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 * L_1 = __this->get_address_of_entityData_3();
L_1->set_attackValue_2(((int32_t)100));
4.2 函数调用
4.2.1 普通成员函数调用
// 普通成员函数
entity.Attack();
生成的 C++ 代码是
BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC * L_7 = ___entity0;
NullCheck(L_7);
BaseEntity_Attack_m38372EF5D100F37FA07CE65CDF09A67122356F06(L_7, /*hidden argument*/NULL);
调用了成员函数对应的 C 语言函数,把实例作为参数传入
4.2.2 静态函数调用
// 静态函数
BaseEntity.GetEntityDesc();
生成的 C++ 代码是
BaseEntity_GetEntityDesc_m9044C9F37CA9BE69015A560E75854C1B16DD8471(/*hidden argument*/NULL);
直接调用了静态函数对应的 C 语言函数,参数传 NULL
4.2.3 虚函数调用
首先我们在 C# 的 BaseEntity 类中增加一个 virtual 方法:
// 虚方法
public virtual void DisplayEntity()
{
Debug.Log("BaseEntity:" + this._entityId);
}
然后实现一个 MonsterEntity 类,重写该虚方法
public class MonsterEntity : BaseEntity
{
public override void DisplayEntity()
{
Debug.Log("MonsterEntity:" + this.EntityId);
}
}
对虚方法的调用代码为
// 虚方法
entity.DisplayEntity();
最终生成的 C++ 调用代码为:
BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC * L_8 = ___entity0;
NullCheck(L_8);
VirtActionInvoker0::Invoke(5 /* System.Void BaseEntity::DisplayEntity() */, L_8);
当自定义代码中有虚函数时,会在生成的 C++ 代码中生成结构体 VirtActionInvoker0
类进行虚函数调用逻辑,这个结构体的完整代码:
struct VirtActionInvoker0
{
typedef void (*Action)(void*, const RuntimeMethod*);
static inline void Invoke (Il2CppMethodSlot slot, RuntimeObject* obj)
{
const VirtualInvokeData& invokeData = il2cpp_codegen_get_virtual_invoke_data(slot, obj);
((Action)invokeData.methodPtr)(obj, invokeData.method);
}
};
在 il2cpp 源码中查看 il2cpp_codegen_get_virtual_invoke_data
实现如下:
FORCE_INLINE const VirtualInvokeData& il2cpp_codegen_get_interface_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj, const RuntimeClass* declaringInterface)
{
Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_interface_invoke_data got called on a non-virtual method");
return il2cpp::vm::Class::GetInterfaceInvokeDataFromVTable(obj, declaringInterface, slot);
}
逻辑是通过对象的 class 数据查虚函数表,获得对应的函数地址等数据,来完成调用的,那么这个虚函数表的数据则是在 class 初始化时进行加载和装配的
4.2.4 接口函数调用
我们先定义一个接口 IRunable
interface IRunable
{
void Run();
}
然后 BaseEntity 实现该接口:
public void Run()
{
Debug.Log("BaseEntity.Run");
}
调用接口方法的代码为
IRunable runner = new BaseEntity();
// 接口方法
runner.Run();
最终生成的 C++ 调用代码为:
BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC * L_9 = (BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC *)il2cpp_codegen_object_new(BaseEntity_t10F88AF9625E7158E20269E2A8CEA034B0C8EEFC_il2cpp_TypeInfo_var);
BaseEntity__ctor_m2555EEA5D80679F2A34DA92DC575088A37BCE2B9(L_9, /*hidden argument*/NULL);
NullCheck(L_9);
InterfaceActionInvoker0::Invoke(0 /* System.Void IRunable::Run() */, IRunable_tD667EDDE8FDF251385C0934B011CBCC07BCCAF9F_il2cpp_TypeInfo_var, L_9);
当自定义代码中有接口函数时,会在生成的 C++ 代码中生成结构体 InterfaceActionInvoker0
类进行接口函数调用逻辑,这个结构体的完整代码:
struct InterfaceActionInvoker0
{
typedef void (*Action)(void*, const RuntimeMethod*);
static inline void Invoke (Il2CppMethodSlot slot, RuntimeClass* declaringInterface, RuntimeObject* obj)
{
const VirtualInvokeData& invokeData = il2cpp_codegen_get_interface_invoke_data(slot, obj, declaringInterface);
((Action)invokeData.methodPtr)(obj, invokeData.method);
}
};
在 il2cpp 源码中查看 il2cpp_codegen_get_virtual_invoke_data
实现如下:
FORCE_INLINE const VirtualInvokeData& il2cpp_codegen_get_interface_invoke_data(Il2CppMethodSlot slot, const RuntimeObject* obj, const RuntimeClass* declaringInterface)
{
Assert(slot != kInvalidIl2CppMethodSlot && "il2cpp_codegen_get_interface_invoke_data got called on a non-virtual method");
return il2cpp::vm::Class::GetInterfaceInvokeDataFromVTable(obj, declaringInterface, slot);
}
进一步查看 GetInterfaceInvokeDataFromVTable
的实现
static FORCE_INLINE const VirtualInvokeData& GetInterfaceInvokeDataFromVTable(const Il2CppObject* obj, const Il2CppClass* itf, Il2CppMethodSlot slot)
{
const Il2CppClass* klass = obj->klass;
IL2CPP_ASSERT(klass->initialized);
IL2CPP_ASSERT(slot < itf->method_count);
for (uint16_t i = 0; i < klass->interface_offsets_count; i++)
{
if (klass->interfaceOffsets[i].interfaceType == itf)
{
int32_t offset = klass->interfaceOffsets[i].offset;
IL2CPP_ASSERT(offset != -1);
IL2CPP_ASSERT(offset + slot < klass->vtable_count);
return klass->vtable[offset + slot];
}
}
return GetInterfaceInvokeDataFromVTableSlowPath(obj, itf, slot);
}
逻辑是先将 class 类型参数传入,在类型信息中的 interface_offsets
中,根据传入的 接口类型,查找对应的偏移下标,然后根据偏移下标和 slot 从vtable中取出函数指针。
这里可以推测,class类型信息包含了它所实现的所有接口的类型信息,和每个接口的虚成员函数起始offset,在调用时遍历接口信息数组,找到匹配的接口,取出起始offset。再加上该类接口虚函数偏移值 slot 就得到了目标函数的地址。这里可能会有效率问题,当类实现的接口越多,interfaceOffsets
的长度越长,遍历效率会越低。
5. 数组转换
5.1 数组定义的转换
在 BaseEntity 中增加如下的数组定义
public EntityData[] entityDataList;
public int[] values;
public string[] nameList;
在 C++ 代码中会生成三个结构体,声明如下:
// EntityData[]
struct EntityDataU5BU5D_t3526AC472E84BEAC5D1FE2447F0EFC6133C73C9F;
// System.Int32[]
struct Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83;
// System.String[]
struct StringU5BU5D_t933FB07893230EA91C40FF900D5400665E87B14E;
当然也会生成对应的 RuntimeClass* 指针:
extern RuntimeClass* EntityDataU5BU5D_t3526AC472E84BEAC5D1FE2447F0EFC6133C73C9F_il2cpp_TypeInfo_var;
extern RuntimeClass* Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83_il2cpp_TypeInfo_var;
extern RuntimeClass* StringU5BU5D_t933FB07893230EA91C40FF900D5400665E87B14E_il2cpp_TypeInfo_var;
也就是说,针对每一个类型A[],无论A是值类型还是引用类型,C++ 端都会生成一个新的结构体来表示,且结构体名字中都包含 U5BU5D 字符串,下面先看看引用类型的数组 EntityData[] 生成的结构体:
struct EntityDataU5BU5D_t3526AC472E84BEAC5D1FE2447F0EFC6133C73C9F : public RuntimeArray
{
public:
ALIGN_FIELD (8) EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 m_Items[1];
public:
inline EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 GetAt(il2cpp_array_size_t index) const
{
IL2CPP_ARRAY_BOUNDS_CHECK(index, (uint32_t)(this)->max_length);
return m_Items[index];
}
inline EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 * GetAddressAt(il2cpp_array_size_t index)
{
IL2CPP_ARRAY_BOUNDS_CHECK(index, (uint32_t)(this)->max_length);
return m_Items + index;
}
inline void SetAt(il2cpp_array_size_t index, EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 value)
{
IL2CPP_ARRAY_BOUNDS_CHECK(index, (uint32_t)(this)->max_length);
m_Items[index] = value;
}
inline EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 GetAtUnchecked(il2cpp_array_size_t index) const
{
return m_Items[index];
}
inline EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 * GetAddressAtUnchecked(il2cpp_array_size_t index)
{
return m_Items + index;
}
inline void SetAtUnchecked(il2cpp_array_size_t index, EntityData_t1FE2E889F592CFB2CFF47B578B4B09C4D5980503 value)
{
m_Items[index] = value;
}
};
关键点:值类型数组和引用类型数组生成的结构体都派生自 RuntimeArray
,在 il2cpp 源码中搜索,可以得到 RuntimeArray
也就是 Il2CppArray
的定义:
//Warning: Updates to this struct must also be made to IL2CPPArraySize C code
#ifdef __cplusplus
typedef struct Il2CppArray : public Il2CppObject
{
#else
typedef struct Il2CppArray
{
Il2CppObject obj;
#endif //__cplusplus
/* bounds is NULL for szarrays */
Il2CppArrayBounds *bounds;
/* total number of elements of the array */
il2cpp_array_size_t max_length;
} Il2CppArray;
结构体的成员包括:
- 一个 C++ 端 EntityData 类型的数组
- 三对方法,包括检查下标和不检查下标两种实现
获取元素GetAt
/GetAtUnchecked
设置元素SetAt
/SetAtUnchecked
获取元素地址GetAddressAt
/GetAddressAtUnchecked
5.2 访问数组的代码
C# 端增加如下的使用数组的代码
this.values = new int[100];
this.values[1] = 100;
this.nameList = new string[10];
this.nameList[0] = "a";
this.entityDataList = new EntityData[5];
this.entityDataList[0].attackValue = 99;
生成的 C++ 代码,为方便查看,增加了注释:
// 初始化 int 数组
Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83* L_2 = (Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83*)SZArrayNew(Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83_il2cpp_TypeInfo_var, (uint32_t)((int32_t)100));
__this->set_values_6(L_2);
Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83* L_3 = __this->get_values_6();
NullCheck(L_3);
// 设置下标为1的元素值
(L_3)->SetAt(static_cast(1), (int32_t)((int32_t)100));
// 初始化 string 数组
StringU5BU5D_t933FB07893230EA91C40FF900D5400665E87B14E* L_4 = (StringU5BU5D_t933FB07893230EA91C40FF900D5400665E87B14E*)SZArrayNew(StringU5BU5D_t933FB07893230EA91C40FF900D5400665E87B14E_il2cpp_TypeInfo_var, (uint32_t)((int32_t)10));
__this->set_nameList_7(L_4);
StringU5BU5D_t933FB07893230EA91C40FF900D5400665E87B14E* L_5 = __this->get_nameList_7();
NullCheck(L_5);
ArrayElementTypeCheck (L_5, _stringLiteral86F7E437FAA5A7FCE15D1DDCB9EAEAEA377667B8);
// 设置下标为0的元素值
(L_5)->SetAt(static_cast(0), (String_t*)_stringLiteral86F7E437FAA5A7FCE15D1DDCB9EAEAEA377667B8);
// 初始化自定义类型数组
EntityDataU5BU5D_t3526AC472E84BEAC5D1FE2447F0EFC6133C73C9F* L_6 = (EntityDataU5BU5D_t3526AC472E84BEAC5D1FE2447F0EFC6133C73C9F*)SZArrayNew(EntityDataU5BU5D_t3526AC472E84BEAC5D1FE2447F0EFC6133C73C9F_il2cpp_TypeInfo_var, (uint32_t)5);
__this->set_entityDataList_5(L_6);
EntityDataU5BU5D_t3526AC472E84BEAC5D1FE2447F0EFC6133C73C9F* L_7 = __this->get_entityDataList_5();
NullCheck(L_7);
// 调用元素方法
((L_7)->GetAddressAt(static_cast(0)))->set_attackValue_2(((int32_t)99));
内存空间由方法 SZArrayNew
来完成,查找其实现可以发现,最终是通过 Array::NewSpecific
来实现的分配:
inline RuntimeArray* SZArrayNew(RuntimeClass* arrayType, uint32_t length)
{
return il2cpp::vm::Array::NewSpecific(arrayType, length);
}
6. 泛型的转换
6.1 泛型定义的转换
在 C# 的 BaseEntity 中添加如下的代码
public List intList = new List();
public List floatList = new List();
public List gameObjectList = new List();
public List transformList = new List();
在 C++ 代码的开头生成了几个 struct 类型声明和相关的 RuntimeClass*, 如下:
// System.Collections.Generic.List`1
struct List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226;
// System.Collections.Generic.List`1
struct List_1_t05CC3C859AB5E6024394EF9A42E3E696628CA02D;
// System.Collections.Generic.List`1
struct List_1_t026D7A8C4D989218772DB3E051A624F753A60859;
// System.Collections.Generic.List`1
struct List_1_t3D4152882C54B77C712688E910390D5C8E030463;
// System.Collections.Generic.List`1
struct List_1_t4DBFD85DCFB946888856DBE52AC08C2AF69C4DBE;
以 List
生成的结构体代码为例进行分析:
#ifndef LIST_1_TE1526161A558A17A39A8B69D8EEF3801393B6226_H
#define LIST_1_TE1526161A558A17A39A8B69D8EEF3801393B6226_H
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Winvalid-offsetof"
#pragma clang diagnostic ignored "-Wunused-variable"
#endif
// System.Collections.Generic.List`1
struct List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226 : public RuntimeObject
{
public:
// T[] System.Collections.Generic.List`1::_items
Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83* ____items_1;
// System.Int32 System.Collections.Generic.List`1::_size
int32_t ____size_2;
// System.Int32 System.Collections.Generic.List`1::_version
int32_t ____version_3;
// System.Object System.Collections.Generic.List`1::_syncRoot
RuntimeObject * ____syncRoot_4;
public:
inline static int32_t get_offset_of__items_1() { return static_cast(offsetof(List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226, ____items_1)); }
inline Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83* get__items_1() const { return ____items_1; }
inline Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83** get_address_of__items_1() { return &____items_1; }
inline void set__items_1(Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83* value)
{
____items_1 = value;
Il2CppCodeGenWriteBarrier((&____items_1), value);
}
inline static int32_t get_offset_of__size_2() { return static_cast(offsetof(List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226, ____size_2)); }
inline int32_t get__size_2() const { return ____size_2; }
inline int32_t* get_address_of__size_2() { return &____size_2; }
inline void set__size_2(int32_t value)
{
____size_2 = value;
}
inline static int32_t get_offset_of__version_3() { return static_cast(offsetof(List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226, ____version_3)); }
inline int32_t get__version_3() const { return ____version_3; }
inline int32_t* get_address_of__version_3() { return &____version_3; }
inline void set__version_3(int32_t value)
{
____version_3 = value;
}
inline static int32_t get_offset_of__syncRoot_4() { return static_cast(offsetof(List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226, ____syncRoot_4)); }
inline RuntimeObject * get__syncRoot_4() const { return ____syncRoot_4; }
inline RuntimeObject ** get_address_of__syncRoot_4() { return &____syncRoot_4; }
inline void set__syncRoot_4(RuntimeObject * value)
{
____syncRoot_4 = value;
Il2CppCodeGenWriteBarrier((&____syncRoot_4), value);
}
};
struct List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226_StaticFields
{
public:
// T[] System.Collections.Generic.List`1::_emptyArray
Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83* ____emptyArray_5;
public:
inline static int32_t get_offset_of__emptyArray_5() { return static_cast(offsetof(List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226_StaticFields, ____emptyArray_5)); }
inline Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83* get__emptyArray_5() const { return ____emptyArray_5; }
inline Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83** get_address_of__emptyArray_5() { return &____emptyArray_5; }
inline void set__emptyArray_5(Int32U5BU5D_t2B9E4FDDDB9F0A00EC0AC631BA2DA915EB1ECF83* value)
{
____emptyArray_5 = value;
Il2CppCodeGenWriteBarrier((&____emptyArray_5), value);
}
};
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#endif // LIST_1_TE1526161A558A17A39A8B69D8EEF3801393B6226_H
相当于将 List
作为一个 class 生成的数据,包括
- 一个 struct,罗列了相关的成员变两个和四件套方
- 一个 _StaticFields 的 struct
- 代码中有调用的泛型成员方法,生成对应的 C 函数:
// System.Void System.Collections.Generic.List`1::Add(!0)
inline void List_1_Add_m50C0D1F69B2EF31137658E2F052EBBAC7BF82771 (List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226 * __this, int32_t p0, const RuntimeMethod* method)
{
(( void (*) (List_1_tE1526161A558A17A39A8B69D8EEF3801393B6226 *, int32_t, const RuntimeMethod*))List_1_Add_m50C0D1F69B2EF31137658E2F052EBBAC7BF82771_gshared)(__this, p0, method);
}
// System.Void System.Collections.Generic.List`1::Add(!0)
inline void List_1_Add_mA57D25369B82E3B6A1A12D0872C1A80A8CA0B4D3 (List_1_t026D7A8C4D989218772DB3E051A624F753A60859 * __this, float p0, const RuntimeMethod* method)
{
(( void (*) (List_1_t026D7A8C4D989218772DB3E051A624F753A60859 *, float, const RuntimeMethod*))List_1_Add_mA57D25369B82E3B6A1A12D0872C1A80A8CA0B4D3_gshared)(__this, p0, method);
}
引用类型 List
生成的代码几乎和 List
一样,这里引用类型和值类型当做泛型参数时唯一的区别在于 泛型共享,值类型的泛型会展开多分成员函数代码,而引用类型的成员函数最终会共享 System.Object
的函数代码,当使用时从 class 元数据中查找对应的类型参数
下面的方法可以看到,引用类型做 List 的泛型参数时,构造方法最终调用的都是 System.Object
对应的 ctor
方法 List_1__ctor_mC832F1AC0F814BAEB19175F5D7972A7507508BC3_gshared
:
// System.Void System.Collections.Generic.List`1::.ctor()
inline void List_1__ctor_mE0CF797BC1662A4FDFF8009E76AC0A5CD1BB1FCA (List_1_t3D4152882C54B77C712688E910390D5C8E030463 * __this, const RuntimeMethod* method)
{
(( void (*) (List_1_t3D4152882C54B77C712688E910390D5C8E030463 *, const RuntimeMethod*))List_1__ctor_mC832F1AC0F814BAEB19175F5D7972A7507508BC3_gshared)(__this, method);
}
// System.Void System.Collections.Generic.List`1::.ctor()
inline void List_1__ctor_m0046B0A356552D7B951C2CEEE5F1375DAF72054A (List_1_t4DBFD85DCFB946888856DBE52AC08C2AF69C4DBE * __this, const RuntimeMethod* method)
{
(( void (*) (List_1_t4DBFD85DCFB946888856DBE52AC08C2AF69C4DBE *, const RuntimeMethod*))List_1__ctor_mC832F1AC0F814BAEB19175F5D7972A7507508BC3_gshared)(__this, method);
}
// System.Void System.Collections.Generic.List`1::.ctor()
inline void List_1__ctor_m9DDA0653CB686964048A019E05AAB4209FF4F82C (List_1_tA533270AE10FC1FF78FC9AE251B64C572B58199A * __this, const RuntimeMethod* method)
{
(( void (*) (List_1_tA533270AE10FC1FF78FC9AE251B64C572B58199A *, const RuntimeMethod*))List_1__ctor_mC832F1AC0F814BAEB19175F5D7972A7507508BC3_gshared)(__this, method);
}
查找该方法的实现:
// System.Void System.Collections.Generic.List`1::.ctor()
extern "C" IL2CPP_METHOD_ATTR void List_1__ctor_mC832F1AC0F814BAEB19175F5D7972A7507508BC3_gshared (List_1_t05CC3C859AB5E6024394EF9A42E3E696628CA02D * __this, const RuntimeMethod* method)
{
{
NullCheck((RuntimeObject *)__this);
Object__ctor_m925ECA5E85CA100E3FB86A4F9E15C120E9A184C0((RuntimeObject *)__this, /*hidden argument*/NULL);
IL2CPP_RUNTIME_CLASS_INIT(IL2CPP_RGCTX_DATA(method->klass->rgctx_data, 0));
ObjectU5BU5D_t3C9242B5C88A48B2A5BD9FDA6CD0024E792AF08A* L_0 = ((List_1_t05CC3C859AB5E6024394EF9A42E3E696628CA02D_StaticFields*)il2cpp_codegen_static_fields_for(IL2CPP_RGCTX_DATA(method->klass->rgctx_data, 0)))->get__emptyArray_5();
__this->set__items_1(L_0);
return;
}
}
关键的代码显示,根据实例参数 method->klass->rgctx_data
来获得类型信息。