Skip to content

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

@hoxily

Description

@hoxily

主工程类型定义:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions