首页 > 解决方案 > Unity - Windows 中出现“空对象引用”错误,但 macOS 中没有

问题描述

我正在与其他开发人员一起制作 2D 游戏。我是唯一一个使用 macOS 机器的人,而我的其他朋友正在使用 Windows 机器。

我做了一个递归函数,从对象中提取底层原始字段及其数据。

在处理 _syncroot 变量时,遇到传递具有空 List<> 对象(包含 0 个项目的列表)的对象时似乎会导致错误。

public List<KeyValuePair<FieldInfo, object>> FlattenAndGetPrimitiveFields(object o)
{
    List<KeyValuePair<FieldInfo, object>> l = new List<KeyValuePair<FieldInfo, object>>();
    foreach (FieldInfo f in o.GetType().GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance))
    {
        if (!f.FieldType.IsPrimitive && f.FieldType != typeof(string) && f.FieldType.BaseType != typeof(System.Enum))
        {
            //Debug.Log(f);
            //Debug.Log(o);
            object nonPrimitiveField = f.GetValue(o);
            // Can use the AddRange method to make this faster.
            l = l.Concat(FlattenAndGetPrimitiveFields(nonPrimitiveField)).ToList();
        }
        else
        {
            KeyValuePair<FieldInfo, object> p = new KeyValuePair<FieldInfo, object>(f,o);
            l.Add(p);
        }
    }
    return l;
}

我只想获取底层的原始字段 - 但是,此代码已经适用于我的 macOS 机器,而不适用于我朋友的 Windows 机器。

他们正在运行 .NET Framework,而我安装了 Mono Framework 和 .NET Core。

标签: c#.netunity3d.net-core

解决方案


推荐阅读