Ourpalm / ILRuntimeU3D

Unity3D demo project for ILRuntime
506 stars 217 forks source link

如果主工程中JSONObject类做了CLR绑定、Data类未作CLR绑定,那么在热更工程里结合使用这两个类时出现InvalidCastException: Specified cast is not valid. #24

Open hoxily opened 4 days ago

hoxily commented 4 days ago

主工程类型定义:

/// <summary>
/// dummy JSONObject class.
/// </summary>
public class JSONObject
{
    public bool GetField(out bool field, string name, bool default_value)
    {
        field = default_value;
        return true;
    }

    public bool GetField(out int field, string name, int default_value)
    {
        field = default_value;
        return true;
    }
}

/// <summary>
/// example Data class.
/// </summary>
public class Data
{
    public int _int_value;
    public bool _bool_value;
}

热更工程中调用:

// static method
public static void StaticFunTest()
{
    UnityEngine.Debug.Log("!!! InstanceClass.StaticFunTest()");

    try
    {
        JSONObject json = new JSONObject();
        Data data = new Data();

        json.GetField(out data._int_value, nameof(data._int_value), 0);
        UnityEngine.Debug.Log("parse int value success.");
        json.GetField(out data._bool_value, nameof(data._bool_value), false);
        UnityEngine.Debug.Log("parse bool value success.");
    }
    catch (Exception e)
    {
        UnityEngine.Debug.LogError("parse data error.");
        UnityEngine.Debug.LogException(e);
    }
}
hoxily commented 4 days ago

示例工程:https://github.com/hoxily/ILRuntimeU3D/commit/3831ea0e2e8d9c5bb49b2ce9d8c2934a4da99f22 主要改动:

  1. 添加文件 JSONObject.cs;
  2. 修改 InstanceClass.cs;

其他变动为升级Unity版本和CLR自动分析生成物。